美文网首页
React-Native之React Navigation

React-Native之React Navigation

作者: SnoopPanda | 来源:发表于2019-08-20 15:34 被阅读0次
    React Navigation官方地址: https://reactnavigation.org/

    设置样式和标题:

      static navigationOptions = {
        title: 'Home', 
        headerStyle: {
          backgroundColor: '#f4511e',
        },
        headerTintColor: '#fff',
        headerTitleStyle: {
          fontWeight: 'bold',
        },
      };
    

    可变Title:

      static navigationOptions = ({ navigation }) => {
        return {
          title: navigation.getParam('otherParam', 'A Nested Details Screen'),
        };
      };
    
      /* Inside of render() */
      <Button
        title="Update the title"
        onPress={() => this.props.navigation.setParams({otherParam: 'Updated!'})}
      />
    

    统一设置样式

    const RootStack = createStackNavigator(
      {
        Home: HomeScreen,
        Details: DetailsScreen,
      },
      {
        initialRouteName: 'Home',
        /* The header config from HomeScreen is now here */
        defaultNavigationOptions: {
          headerStyle: {
            backgroundColor: '#f4511e',
          },
          headerTintColor: '#fff',
          headerTitleStyle: {
            fontWeight: 'bold',
          },
        },
      }
    );
    

    可变样式

    class DetailsScreen extends React.Component {
      static navigationOptions = ({ navigation, navigationOptions }) => {
        const { params } = navigation.state;
    
        return {
          title: params ? params.otherParam : 'A Nested Details Screen',
          /* These values are used instead of the shared configuration! */
          headerStyle: {
            backgroundColor: navigationOptions.headerTintColor,
          },
          headerTintColor: navigationOptions.headerStyle.backgroundColor,
        };
      };
    
      /* render function, etc */
    }
    

    自定义TitleView

    class LogoTitle extends React.Component {
      render() {
        return (
          <Image
            source={require('./spiro.png')}
            style={{ width: 30, height: 30 }}
          />
        );
      }
    }
    
    class HomeScreen extends React.Component {
      static navigationOptions = {
        // headerTitle instead of title
        headerTitle: <LogoTitle />,
      };
    
      /* render function, etc */
    }
    

    相关文章

      网友评论

          本文标题:React-Native之React Navigation

          本文链接:https://www.haomeiwen.com/subject/qkhpsctx.html