Authored by 叶弯弯

修复NavBar在Android上的一些兼容问题。reviewed by yuliang。

@@ -35,6 +35,7 @@ import { @@ -35,6 +35,7 @@ import {
35 TouchableOpacity, 35 TouchableOpacity,
36 View, 36 View,
37 Dimensions, 37 Dimensions,
  38 + BackAndroid,
38 } from 'react-native'; 39 } from 'react-native';
39 import {Actions} from 'react-native-router-flux'; 40 import {Actions} from 'react-native-router-flux';
40 import _drawerImage from '../images/menu_burger.png'; 41 import _drawerImage from '../images/menu_burger.png';
@@ -50,7 +51,7 @@ const styles = StyleSheet.create({ @@ -50,7 +51,7 @@ const styles = StyleSheet.create({
50 fontWeight: '500', 51 fontWeight: '500',
51 color: '#0A0A0A', 52 color: '#0A0A0A',
52 position: 'absolute', 53 position: 'absolute',
53 - top: Platform.OS === 'ios' || Platform.Version > 19 ? 20 : 0, 54 + top: Platform.OS === 'ios' ? 20 : 5,
54 left: 0, 55 left: 0,
55 right: 0, 56 right: 0,
56 backgroundColor: 'transparent', 57 backgroundColor: 'transparent',
@@ -59,7 +60,7 @@ const styles = StyleSheet.create({ @@ -59,7 +60,7 @@ const styles = StyleSheet.create({
59 backgroundColor: '#EFEFF2', 60 backgroundColor: '#EFEFF2',
60 paddingTop: 0, 61 paddingTop: 0,
61 top: 0, 62 top: 0,
62 - height: Platform.OS === 'ios' || Platform.Version > 19 ? 64 : 44, 63 + height: Platform.OS === 'ios' ? 64 : 50,
63 right: 0, 64 right: 0,
64 left: 0, 65 left: 0,
65 borderBottomWidth: 0.5, 66 borderBottomWidth: 0.5,
@@ -175,6 +176,27 @@ class NavBar extends React.Component { @@ -175,6 +176,27 @@ class NavBar extends React.Component {
175 this.renderTitle = this.renderTitle.bind(this); 176 this.renderTitle = this.renderTitle.bind(this);
176 } 177 }
177 178
  179 + componentWillMount() {
  180 + if (Platform.OS === 'android') {
  181 + BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid);
  182 + }
  183 + }
  184 +
  185 + componentWillUnmount() {
  186 + if (Platform.OS === 'android') {
  187 + BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid);
  188 + }
  189 + }
  190 +
  191 + onBackAndroid = () => {
  192 + let sceneKey = this.props.navigationState.sceneKey;
  193 + if (CONFIG.sceneKey.root === sceneKey) {
  194 + Actions.pop();
  195 + return true;
  196 + }
  197 + return false;
  198 + }
  199 +
178 renderBackButton() { 200 renderBackButton() {
179 const state = this.props.navigationState; 201 const state = this.props.navigationState;
180 const childState = state.children[state.index]; 202 const childState = state.children[state.index];
@@ -407,13 +429,13 @@ class NavBar extends React.Component { @@ -407,13 +429,13 @@ class NavBar extends React.Component {
407 if (selected.hideNavBar) { 429 if (selected.hideNavBar) {
408 return null; 430 return null;
409 } 431 }
410 - 432 +
411 while (selected.hasOwnProperty('children')) { 433 while (selected.hasOwnProperty('children')) {
412 state = selected; 434 state = selected;
413 selected = selected.children[selected.index]; 435 selected = selected.children[selected.index];
414 } 436 }
415 437
416 - 438 +
417 439
418 const renderLeftButton = selected.renderLeftButton || 440 const renderLeftButton = selected.renderLeftButton ||
419 selected.component.renderLeftButton || 441 selected.component.renderLeftButton ||
@@ -437,10 +459,11 @@ class NavBar extends React.Component { @@ -437,10 +459,11 @@ class NavBar extends React.Component {
437 this.props.navigationBarStyle, 459 this.props.navigationBarStyle,
438 state.navigationBarStyle, 460 state.navigationBarStyle,
439 selected.navigationBarStyle, 461 selected.navigationBarStyle,
  462 + CONFIG.sceneKey.user === sceneKey && styles.imageMy,
440 ]} 463 ]}
441 > 464 >
442 - <Image  
443 - source={CONFIG.sceneKey.user === sceneKey ? require('../images/mybg.png') : require('../images/navbar.png')} 465 + <Image
  466 + source={CONFIG.sceneKey.user === sceneKey ? require('../images/mybg.png') : require('../images/navbar.png')}
444 style={[styles.image, CONFIG.sceneKey.user === sceneKey && styles.imageMy]} 467 style={[styles.image, CONFIG.sceneKey.user === sceneKey && styles.imageMy]}
445 > 468 >
446 {renderTitle ? renderTitle(selected) : state.children.map(this.renderTitle, this)} 469 {renderTitle ? renderTitle(selected) : state.children.map(this.renderTitle, this)}