简体   繁体   English

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

[英]how to show navigation bar using navigator component in react-native

Hi every one I am trying to use navigator component in place of navigatorIOS , but what the problem navigator is not showing in view, below is my code for navigator and navigatorIOS 大家好我试图使用navigator组件代替navigatorIOS ,但问题navigator器没有显示在视图中,下面是我的navigatornavigatorIOS代码

NavigatorIOS: NavigatorIOS:

<NavigatorIOS
     style={styles.navigator}
     barTintColor='#37475e'
     titleTextColor='white'
     initialRoute={{
     title:' Words',
     component:Main
}}/>

using navigatorIOS I can view the navbar but using using navigator Component I am facing problem I want to use navigator component instead of navigatorIOS 使用navigatorIOS我可以查看navbar但使用navigator组件我面临问题我想使用navigator组件而不是navigatorIOS

Navigator Component: 导航器组件:

<Navigator
        style={styles.navigator}
        TintColor='#37475e'
        titleTextColor='white'
        initialRoute={{
          title:'Words',
          component:Main
        }}
        renderScene={(route, navigator) =>{
          return <route.component navigator={navigator} {...route.passProps} />;
}}/>

Can any one give me suggestions for how to solve this any help much appreciated 可以任何人给我建议如何解决这个任何帮助非常感谢

Yes, you need to create a <Navigator.NavigationBar /> , and pass it in as a prop to the Navigator : 是的,您需要创建一个<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} /> } 
       />
    )
  }
}

You then need to create a routeMapper object, which you pass in as a prop to the Navigator.NavigationBar : 然后,您需要创建一个routeMapper对象,您将其作为prop传递给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>
   }
};

There is a great example in the UI Explorer in their GitHub, check it out here . 在他们的GitHub的UI资源管理器中有一个很好的例子,请在这里查看

App.js 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} />
                    }
                />

This code for left, right and title on navigationBar in react-native 这个代码在navigationBar中的left,right和title in 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(): method to call right button click. route.onPress():调用右键单击的方法。

route.title: set title of page. route.title:设置页面标题。

For reference, use following link: Navigator 供参考,请使用以下链接: Navigator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM