如何在react-native中使用navigator组件显示导航栏

时间:2016-02-11 13:23:43

标签: react-native navigator

我希望每个人都尝试使用navigator组件代替navigatorIOS,但问题navigator未显示在视图中,下面是{{1}的代码}和navigator

NavigatorIOS:

navigatorIOS

使用<NavigatorIOS style={styles.navigator} barTintColor='#37475e' titleTextColor='white' initialRoute={{ title:' Words', component:Main }}/> 我可以查看navigatorIOS但使用navbar组件我遇到问题我想使用navigator组件而不是navigator

导航器组件:

navigatorIOS

任何人都可以给我建议如何解决这个问题,非常感谢

2 个答案:

答案 0 :(得分:2)

是的,您需要创建<Navigator.NavigationBar />,并将其作为道具传递给Navigator

class App extends React.Component {
  render() {
      return (
        <Navigator
          style={{flex:1}}
          initialRoute={{name: 'Main', component: Main, index: 0}}
          renderScene={(route, navigator) =>    {
            if (route.component) {
              return React.createElement(route.component, { ...this.props, ...route.passProps, navigator, route } );
             }
          }}
          navigationBar={ <Navigator.NavigationBar routeMapper={NavigationBarRouteMapper} /> } 
       />
    )
  }
}

然后,您需要创建一个routeMapper对象,并将其作为道具传递给Navigator.NavigationBar

var NavigationBarRouteMapper = {
  LeftButton(route, navigator, index, navState) {
    if(index > 0) {
      return (
      <TouchableHighlight style={{marginTop: 10}} onPress={() => {
            if (index > 0) {
              navigator.pop();
            } 
        }}>
       <Text>Back</Text>
     </TouchableHighlight>
   )} else {
   return null}
   },
   RightButton(route, navigator, index, navState) {
      return null;
   },
   Title(route, navigator, index, navState) {
      return <Text>Hello From My App!</Text>
   }
};

在UI资源管理器中有一个很好的例子,请查看here

答案 1 :(得分:1)

<强> App.js

<Navigator
                    ref={(ref) => this._navigator = ref}
                    configureScene={(route) => Navigator.SceneConfigs.FloatFromLeft}
                    initialRoute={{
                        id: 'Main',
                        title: 'Words'
                    }}
                    renderScene={(route, navigator) => this._renderScene(route, navigator)}
                    navigationBar={
                        <Navigator.NavigationBar
                            style={styles.navBar}
                            routeMapper={NavigationBarRouteMapper} />
                    }
                />

navigationBar

react-native上左,右和标题的代码
const NavigationBarRouteMapper = {
    LeftButton(route, navigator, index, navState) {
        switch (route.id) {
            case 'Page1':
            case 'Page2':
            case 'Page3':
                return (
                    <TouchableOpacity
                        style={styles.navBarLeftButton}
                        onPress={() => {_emitter.emit('openMenu')}}>
                        <Icon name='menu' size={25} color={'white'} />
                    </TouchableOpacity>
                )
            default:
                return null
        }
    },
    RightButton(route, navigator, index, navState) {
      switch (route.id) {
          case 'Page1':
            return (
            <TouchableOpacity
                style={styles.navBarRightButton} onPress={() => {route.onPress()}}>
                <Icon name={'map'} size={25} color={'white'} />
            </TouchableOpacity>
            )
            case 'Page2':
              return (
              <TouchableOpacity
                  style={styles.navBarRightButton} onPress={() => {route.onPress()}}>
                  <Icon name={'map'} size={25} color={'white'} />
              </TouchableOpacity>
              )
            case 'Page3':
              return (
                <TouchableOpacity
                    style={styles.navBarRightButton} onPress={() => {route.onPress()}}>
                    <Icon name={'tab'} size={25} color={'white'} />
                </TouchableOpacity>
                )

            default:
              return null
          }
    },
    Title(route, navigator, index, navState) {
        return (
            <Text style={[styles.navBarText, styles.navBarTitleText]}>
                {route.title}
            </Text>
        )
    }
}

route.onPress():方法调用右键单击。

route.title:设置页面标题。

供参考,请使用以下链接:Navigator