【问题标题】:Disable Swipe of Parent TabNavigator when in certain Screens in Nested Navigators在嵌套导航器的某些屏幕中禁用父 TabNavigator 的滑动
【发布时间】:2017-11-04 18:24:44
【问题描述】:

我一直在尝试为我的应用实现嵌套导航器。 应用程序的结构非常简单。

RootNavigation(TabNavigator):
    - Camera
    - HomeNavigation (TabNavigator)
    - MessagesList

所以应用程序默认在 HomeNavigation 上启动。向左滑动查看Camera,向右滑动查看MessagesList

现在HomeNavigation 进一步划分如下:

HomeNavigation (TabNavigator):
    - News Feed List
    - NotificationsList
    - Search News

在此导航器中,News Feed List 默认显示。现在,当我转到Home Navigation 中的NotificationsListSearch News 时。向左或向右滑动会将我带到我不想要的 CameraMessagesList。当我在News Feed List 时,我只想要这个功能。现在我正在阅读文档,但不太明白如何在RootNavigation (Parent Navigator) from inside the child 中禁用滑动。同样在开发阶段的后期,News Feed List 将转换为StackNavigator,用户可以在其中选择每个新闻项目并详细阅读。我还希望在深入NewsFeedList 时禁用它。

也许这段代码将有助于更好地理解我的情况:

HomeNavigationConfigurtation:

const homeNavigationConfiguration = {
    TabOneNavigation: {
        screen: FeedScreen,
        navigationOptions: {
            tabBarLabel: 'Home',
            tabBarIcon: ({ tintColor, focused }) => {
                return (
                    <Ionicons
                        name={focused ? 'ios-home' : 'ios-home-outline'}
                        size={30}
                        style={{ color: tintColor }}
                    />
                );
            }
        }
    },
    TabTwoNavigation: {
        screen: SearchTab,
        navigationOptions: {
            tabBarLabel: 'Search',
            tabBarIcon: ({ tintColor, focused }) => {
                return (
                    <Ionicons
                        name={focused ? 'ios-search' : 'ios-search-outline'}
                        size={30}
                        style={{ color: tintColor }}
                    />
                );
            }
        }
    },
    TabThreeNavigation: {
        screen: NotificationTab,
        navigationOptions: {
            tabBarLabel: 'Notify',
            tabBarIcon: ({ tintColor, focused }) => {
                return (
                    <Ionicons
                        name={focused ? 'ios-list' : 'ios-list-outline'}
                        size={30}
                        style={{ color: tintColor }}
                    />
                );
            }
        }
    }
};

const tabBarConfiguration = {
    //...other configs
    tabBarComponent: TabBarBottom,
    tabBarPosition: 'bottom',
    swipeEnabled: false,
    animationEnabled: false,
    lazy: true,
    tabBarOptions: {
        showIcon: true,
        showLabel: false,
        activeTintColor: '#347FC4',
        inactiveTintColor: '#929AA1',
        activeBackgroundColor: '#FFF',
        inactiveBackgroundColor: '#FFF'
    }
};

export const HomeNavConfiguration = TabNavigator(routeConfiguration, tabBarConfiguration);

主页导航:

class HomeNavigationextends Component {
    render() {
        const { dispatch, navigationState } = this.props;
        return (
            <HomeNavigationConfiguration
                navigation={
                    addNavigationHelpers({
                        dispatch,
                        state: navigationState,
                    })
                }
            />
        );
    }
}

const mapStateToProps = (state) => {
    return {
        navigationState: state.homeNavState,
    };
};

export default connect(mapStateToProps)(HomeNavigation);

HomeNavigationReducer:

export default (state, action) => {
    if (action.type === 'JUMP_TO_TAB') {
        return { ...state, index: 0 };
    }
    return HomeNAvigationConfig.router.getStateForAction(action, state);
};

RootNavigationConfiguration:

const routeConfiguration = {
    RootNavigationScreenLeft: {
        screen: CameraScreen,
        navigationOptions: {
            tabBarVisible: false
        }
    },
    RootNavigationScreenMiddle: {
        screen: RootTab,
        navigationOptions: {
            tabBarVisible: false
        }
    },
    RootNavigationScreenRight: {
        screen: MessagesListScreen,
        navigationOptions: {
            tabBarVisible: false
        }
    }
};

const tabBarConfiguration = {
    //...other configs
    swipeEnabled: true,
    animationEnabled: true,
    lazy: true,
    initialRouteName: 'RootNavigationScreenMiddle'
};

export const RootNavigationConfig = TabNavigator(routeConfiguration, tabBarConfiguration);

根导航

class RootNavigationextends React.Component {
    render() {
        const { dispatch, navigationState } = this.props;
        return (
            <RootNavigationConfig
                navigation={
                    addNavigationHelpers({
                        dispatch,
                        state: navigationState,
                    })
                }
            />
        );
    }
}

const mapStateToProps = (state) => {
    return {
        navigationState: state.rootNavState,
    };
};

export default connect(mapStateToProps)(RootNavigation);

我们将不胜感激任何解决方案或建议。 :)

【问题讨论】:

    标签: react-native react-redux react-navigation


    【解决方案1】:

    从用户体验的角度来看,用户可能会感到困惑。你基本上在做的是,

    RootNavigation (TabNavigator)
    - Camera
    - News Feed List
    - NotificationsList
    - Search News
    - MessagesList
    

    考虑一下这是否适用于您的情况?

    RootNavigation(StackNavigator):
        - Camera
        - HomeNavigation (TabNavigator)
        - MessagesList
    
    HomeNavigation (TabNavigator):
        - News Feed List
        - NotificationsList
        - Search News
    

    至于你的问题,

    我只有在News Feed List时才想要这个功能

    当您设置导航时,其格式为TabNavigator(RouteConfigs, TabNavigatorConfig)

    您可以将navigationOptions 传递给RouteConfigsTabNavigatorConfig。您在每个screen 对象中指定的navigationOptions 允许覆盖TabNavigatorConfig 中的navigationOptions。基本上,您将所有屏幕的“通用”配置传递给 TabNavigatorConfig navigationOptionsscreen-specificRouteConfigs navigationOptions

    【讨论】:

    • 如果我使用StackNavigator,会不会失去在RootNavigation中左右滑动访问Camera和MessagesList的选项?
    • 哦,是的。但是,这只是一个建议(混合 Stack 和 Tab navigator),可能是为了更简洁的 UX 设计。如果您仍然希望在所有屏幕之间滑动,为什么不简单地将所有 5 个屏幕仅包裹在 TabNavigator 下? HomeNavigation 分类似乎是多余的。
    • 好吧,我首先这样做了,然后我也将 StackNavigator 保留为 RootNavigation,但左右滑动似乎比​​拥有一个可以完成相同工作的按钮更好,所以我将 RootNavigation 更改为 TabNavigator。当您看到相机进入时,它会提供更好的动画效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多