React Navigation - StackNavigato

2017-06-01  本文已影响0人  xinayida

为你的应用提供一种切换屏幕的方式,新创建的屏幕放置在栈顶。
默认情况下,StackNavigator被配置为在iOS和Android上有类似的外观和体验:iOS上新页面从右边划入,Android为从底部淡入。iOS上StackNavigator还可以配置为modal风格,新页面从底部划入。

class MyHomeScreen extends React.Component {
  static navigationOptions = {
    title: 'Home',
  }

  render() {
    return (
      <Button
        onPress={() => this.props.navigation.navigate('Profile', {name: 'Lucy'})}
        title="Go to Lucy's profile"
      />
    );
  }
}

const ModalStack = StackNavigator({
  Home: {
    screen: MyHomeScreen,
  },
  Profile: {
    path: 'people/:name',
    screen: MyProfileScreen,
  },
});

API 定义

StackNavigator(RouteConfigs, StackNavigatorConfig)

路径配置

路径配置对象为路径名到路径配置的映射,从而告诉navigator当前的路径下展示什么页面。

StackNavigator({

  // 每一个新页面都要创建一个这样的实体:
  Profile: {

    // `ProfileScreen` 是当前页面所要展示的React组件
    screen: ProfileScreen,
    // 当`ProfileScreen`被StackNavigator加载时, 他会被赋予一个`navigation`属性.

    // 可选: deeplink 或 web 应用中使用:
    path: 'people/:name',
    // The action and route params are extracted from the path.

    // 可选: 对当前页重写 `navigationOptions`
    navigationOptions: ({navigation}) => ({
      title: `${navigation.state.params.name}'s Profile'`,
    }),
  },

  ...MyOtherRoutes,
});

StackNavigatorConfig

可选的路由:

可视选项:

页面导航选项

title 作为headerTitle和tabBarLabel的备用字符串
header React 控件或方法给HeaderProps返回一个用来显示header的控件,设置为null时隐藏header。
headerTitle header的标题,字符串或控件,默认为场景标题。
headerBackTitle iOS回退键标题,为空时禁用标签。默认为场景标题。
headerTruncatedBackTitleheaderBackTitle不适合屏幕时用来作为回退键的标题,默认为“Back”
headerRight显示在header右边的控件
headerLeft 显示在header左边的控件
headerStyle header的样式对象
headerTitleStyle header标题的样式对象
headerBackTitleStyle header回退键标题的样式对象
headerTintColor header的tint颜色
headerPressColorAndroid Android5.0以上按键按下的涟漪颜色
gesturesEnabled 是否可以使用手势关闭页面,默认iOS为true,Android为false。

Navigator 属性

navigator组件通过StackNavigator(...)获取一下属性:

const SomeStack = StackNavigator({
  // config
});

<SomeStack
  screenProps={/* this prop will get passed to the screen components as this.props.screenProps */}
/>

例子

See the examples SimpleStack.js and ModalStack.js which you can run locally as part of the NavigationPlayground app.
You can view these examples directly on your phone by visiting our expo demo.

上一篇 下一篇

猜你喜欢

热点阅读