简体   繁体   English

如何在嵌套的 tabNavigation 中编辑每个屏幕的 navigationOptions?

[英]How can I edit navigationOptions for each screen in nested tabNavigation?

I am trying to show title in the header and also a right action button in the header.我试图在 header 中显示标题,还在 header 中显示右操作按钮。

I have tried to follow https://reactnavigation.org/docs/en/navigation-options-resolution.html#a-tab-navigator-contains-a-stack-and-you-want-to-hide-the-tab-bar-on-specific-screens But without any progress.我试图关注https://reactnavigation.org/docs/en/navigation-options-resolution.html#a-tab-navigator-contains-a-stack-and-you-want-to-hide-the-tab -bar-on-specific-screens但没有任何进展。

Navigation.js导航.js

const FeedStack = createStackNavigator({
    Welcome: {
        screen: WelcomeScreen,
        navigationOptions: {
            headerTitle: 'Welcome',
        },
    },
    Auth: {
        screen: AuthScreen,
        navigationOptions: {
            headerTitle: 'Login',
        },
    },
}, {
    defaultNavigationOptions: {
        headerStyle: {
            backgroundColor: Platform.OS === 'android' ? Colors.headerColor : 'white',
        },
        headerTintColor: Platform.OS === 'android' ? 'white' : Colors.secondaryColor,
    },
});

const TabNavigator = createBottomTabNavigator({
    DailyTask: {
        screen: DailyTaskScreen,
        navigationOptions: {
            headerTitle: 'Daily Task',
        },
    },
    MyTasks: {
        screen: MyTasksScreen,
        navigationOptions: {
            headerTitle: 'My Tasks',
        },
    },
    SelectCategory: {
        screen: SelectCategoryScreen,
        navigationOptions: {
            headerTitle: 'Select Category',
        },
    },
}, {
    defaultNavigationOptions: {
        headerStyle: {
            backgroundColor: Platform.OS === 'android' ? Colors.headerColor : 'white',
        },
        headerTintColor: Platform.OS === 'android' ? 'white' : Colors.secondaryColor,
    },
});

const HomeStack = createStackNavigator({
    Tabs: TabNavigator,
}, {
    defaultNavigationOptions: {
        headerStyle: {
            backgroundColor: Platform.OS === 'android' ? Colors.headerColor : 'white',
        },
        headerTintColor: Platform.OS === 'android' ? 'white' : Colors.secondaryColor,
    },
});

const AppNavigator = createSwitchNavigator({
    Auth: FeedStack,
    Home: HomeStack,
}, {
    defaultNavigationOptions: {
        headerStyle: {
            backgroundColor: Platform.OS === 'android' ? Colors.headerColor : 'white',
        },
        headerTintColor: Platform.OS === 'android' ? 'white' : Colors.secondaryColor,
    },
});

export default createAppContainer(AppNavigator);

Screen.js Screen.js

DailyTaskScreen.navigationOptions = {
    headerTitle: 'Daily Task',
    headerRight: (
        <HeaderButtons HeaderButtonComponent={HeaderButton}>
            <Item
                title='Category'
                iconName='md-albums'
                onPress={() => {
                    this.props.navigation.navigate('SelectCategory')
                }}
            />
        </HeaderButtons>
    )
};

What do I need to do to get the expected results of being able to edit the navigationOptions?我需要做什么才能获得能够编辑 navigationOptions 的预期结果?

For adding an element at right of the header and implementing it's onPress, you can do it like this:要在 header 的右侧添加一个元素并在 onPress 上实现它,您可以这样做:

Just add this code below 'export default class' in the js file of the screen where you want to add it.只需在要添加它的屏幕的 js 文件中的“导出默认类”下方添加此代码。

static navigationOptions = {
    headerLeft: null,
    headerRight: (
        <TouchableOpacity
            onPress={() => alert('This is a button!')}>
            <Text style={{color: '#fff', marginRight:12, fontSize: 14}}>Done</Text>
        </TouchableOpacity>
    )
};

暂无
暂无

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

相关问题 屏幕导航选项:如何使用翻译 - Screen navigationOptions: how to use translations 如何使用JQuery / Javascript编辑嵌套列表? - How can I edit nested Lists with JQuery/Javascript? 如何通过静态NavigationOptions将道具从上一个屏幕传递到下一个屏幕? - How to pass props from previous screen to next screen through static navigationOptions? 使用bluebird Promises,我该如何做一个嵌套的? - Using bluebird Promises, how can I do a nested each? 如何删除相互嵌套的 html 元素? - How can I remove html elements that are nested into each other? 如何使用字符串访问嵌套对象的值以便我可以编辑? - How to access nested object value using string so I can edit? 我无法使我的应用使用组件文件中的navigationOptions而不是路由 - I can't make my app use the navigationOptions from the component file instead of the routes 如何在 React 中迭代嵌套的 object(在屏幕中呈现任何数据并以不可变的方式设置 state) - How can I Iterate over nested object in React(rendering any data in the screen and set the state in a immutable way) 我们如何使用 React Native 中的 navigationOptions 在功能组件中将状态作为道具传递? - How can we pass state as props in functional component using navigationOptions in React Native? KineticJS:如何在不降低质量的情况下编辑比我的屏幕/舞台尺寸大得多的图像? - KineticJS: How can I edit an image which is much bigger than my screen/stage size without loosing quality?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM