代码之家  ›  专栏  ›  技术社区  ›  Nauman Tanwir

反应本机:导航选项中的标题样式不起作用

  •  3
  • Nauman Tanwir  · 技术社区  · 7 年前

    route.js

    import React, { Component } from 'react';
    import { View } from 'react-native';
    import { createStackNavigator, createAppContainer } from "react-navigation";
    import SplashScreen from '../screens/SplashScreen';
    import CalendarScreeen from '../screens/CalendarScreen';
    
    const NavStack = createStackNavigator(
        {
            Splash: {
                screen: SplashScreen,
                navigationOptions: {
                    header: null
                },
            },
            Calendar: {
                screen: CalendarScreeen,
                navigationOptions: {
                    title: 'Calendar',
                },
            },
        },
        {
            initialRouteName: 'Calendar',
            navigationOptions: {
                headerStyle: {
                    backgroundColor: '#28F1A6',
                    elevation: 0,
                    shadowOpacity: 0
                },
                headerTintColor: '#333333',
                headerTitleStyle: {
                    fontWeight: 'bold',
                    color: '#ffffff'
                }
            }
           
        }
    
    );
    
    const Routes = createAppContainer(NavStack);
    export default Routes;
    

    现在,我意识到我可以在我的类组件中执行类似的操作

    static navigationOptions = {
      title: 'Chat',
      headerStyle: { backgroundColor: 'red' },
      headerTitleStyle: { color: 'green' },
    }
    

    possible alternative

    但我想从我的生活中实现同样的目标 route.js

    defaultNavigationOptions 就像书中提到的那样 docs

    但是没有运气!!

    2 回复  |  直到 5 年前
        1
  •  24
  •   Aung Myat Hein    7 年前

    默认导航选项

    {
            initialRouteName: 'Calendar',
            defaultNavigationOptions: {
                headerStyle: {
                    backgroundColor: '#28F1A6',
                    elevation: 0,
                    shadowOpacity: 0
                },
                headerTintColor: '#333333',
                headerTitleStyle: {
                    fontWeight: 'bold',
                    color: '#ffffff'
                }
            }
    
    }
    

    它应该会起作用。 https://snack.expo.io/ByGrHdAC7

        2
  •  2
  •   Christopher Agnus Lam    7 年前

    你需要使用 defaultNavigationOptions

    诚然,他们甚至没有在文档中提到他们在v2和v3之间进行了更改!

    https://reactnavigation.org/docs/en/stack-navigator.html

        3
  •  2
  •   MOUTAIROU Bastou Abdel    5 年前

    要设置自定义导航,首先需要定义 选项.导航 在你的App.js中

    App.js

     <Stack.Screen
                     name="ListeMedocItemScreen"
                     component={ListeMedocItemScreen}
                     options={ListeMedocItemScreen.defaultNavigationOptions} // ListeMedocItemScreen.navigationOptions
    
      />
    

    如果你用钩子

    
    const ListeMedocItem = (props) =>{
      //some component
    }
    
    
    ListeMedocItem.defaultNavigationOptions = ({navigation}) => {
    
      return {
        title: 'My home',
        headerStyle: {
          backgroundColor: '#f4511e',
        },
        headerTintColor: '#fff',
        headerTitleStyle: {
          fontWeight: 'bold',
        },
    
     }}
    
    

    或者如果您使用React.Components

    
    static navigationOptions = ({ navigation }) => {
        //return header with Custom View which will replace the original header 
        return {
          header: (
            <View
              style={{
                height: 45,
                marginTop: 20,
                backgroundColor: 'red',
                justifyContent: 'center',
              }}>
              <Text
                style={{
                  color: 'white',
                  textAlign: 'center',
                  fontWeight: 'bold',
                  fontSize: 18,
                }}>
                This is Custom Header
              </Text>
            </View>
          ),
        };
      };
    
        4
  •  1
  •   enthusiastdev    5 年前

    navigationOptions defaultNavigationOptions

        5
  •  -1
  •   MetalYcarus    5 年前

    elevation: 0 在安卓系统中,10不起作用。其他安卓版本也可以。 它将更改为透明标题。

    enter image description here