React Navigation - TabNavigato

2018-01-29  本文已影响57人  急眼的蜗牛

使用TabRouter轻松设置带有多个选项卡的屏幕。

class MyHomeScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Home',
    // Note: By default the icon is only shown on iOS. Search the showIcon option below.
    tabBarIcon: ({ tintColor }) => (
      <Image
        source={require('./chats-icon.png')}
        style={[styles.icon, {tintColor: tintColor}]}
      />
    ),
  };

  render() {
    return (
      <Button
        onPress={() => this.props.navigation.navigate('Notifications')}
        title="Go to notifications"
      />
    );
  }
}

class MyNotificationsScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Notifications',
    tabBarIcon: ({ tintColor }) => (
      <Image
        source={require('./notif-icon.png')}
        style={[styles.icon, {tintColor: tintColor}]}
      />
    ),
  };

  render() {
    return (
      <Button
        onPress={() => this.props.navigation.goBack()}
        title="Go back home"
      />
    );
  }
}

const styles = StyleSheet.create({
  icon: {
    width: 26,
    height: 26,
  },
});

const MyApp = TabNavigator({
  Home: {
    screen: MyHomeScreen,
  },
  Notifications: {
    screen: MyNotificationsScreen,
  },
}, {
  tabBarPosition: 'top',
  animationEnabled: true,
  tabBarOptions: {
    activeTintColor: '#e91e63',
  },
});

定义API

TabNavigator(RouteConfigs, TabNavigatorConfig)

RouteConfigs

路由配置对象是从路由名称到路由配置的映射,它告诉导航器该路由呈现什么,参见StackNavigator的示例

TabNavigatorConfig

几个选项被传递给底层路由器来修改导航逻辑:

tabBarOptions for TabBarBottom (iOS上的默认标签栏)

example:

tabBarOptions: {
  activeTintColor: '#e91e63',
  labelStyle: {
    fontSize: 12,
  },
  style: {
    backgroundColor: 'blue',
  },
}

tabBarOptions for TabBarTop (Android上的默认标签栏)

Example:

tabBarOptions: {
  labelStyle: {
    fontSize: 12,
  },
  tabStyle: {
    width: 100,    
  },
  style: {
    backgroundColor: 'blue',
  },
}

屏幕导航选项(Screen Navigation Options)

title
可以用作headerTitle和tabBarLabel的后退的通用标题。

tabBarVisible
显示或隐藏标签栏是true或false,默认为true。

swipeEnabled
启用或禁用在选项卡之间滑动

tabBarIcon
React元素或方法给定{focused:boolean,tintColor:string}的函数返回一个React.Node,以显示在标签栏中。

tabBarLabel
显示在标签栏或React元素中的标签字符串或给定{focused:boolean,tintColor:string}的函数返回一个React.Node,以在标签栏中显示。未定义时,使用场景title。要隐藏,请参阅上一节中的tabBarOptions.showLabel

tabBarOnPress
回调处理点击事件;参数是一个对象,其中包含:

在转换到下一个场景之前添加一个自定义逻辑(被点击的)是有用的。

Navigator Props

由TabNavigator(...)创建的导航器组件采用
props:

const TabNav = TabNavigator({
  // config
});

<TabNav
  screenProps={/* this prop will get passed to the screen components as this.props.screenProps */}
/>
上一篇 下一篇

猜你喜欢

热点阅读