隐藏嵌套堆栈导航器内部屏幕上的标签栏

发布于 2025-02-12 02:01:25 字数 3637 浏览 0 评论 0原文

我想在嵌套的堆栈导航器中将标签栏隐藏在某些屏幕上。我有一个底部的选项卡栏,使用自定义标签栏,就像这样设置:

const MainAppBottomTabs = createBottomTabNavigator<BottomTabParamList>();
const MainAppBottomTabsNavigator = (): JSX.Element => {
    return (
        <MainAppBottomTabs.Navigator
            initialRouteName="ListScreen"
            screenOptions={{
                headerShown: false
            }}
            tabBar={(props: BottomTabBarProps) => (
                <TabBar
                    state={props.state}
                    navigation={props.navigation}
                    descriptors={props.descriptors}
                    insets={props.insets}
                /> // this is my custom tab bar
            )}>
            <MainAppBottomTabs.Screen
                name="ListScreen"
                component={ListScreen}
            />
            <MainAppBottomTabs.Screen
                name="ProductsScreen"
                component={ProductsScreen}
            />
            <MainAppBottomTabs.Screen
                name="DetailsStackNavigator"
                component={DetailsStackNavigator}
            />
        </MainAppBottomTabs.Navigator>
    );
};

这是嵌套delectStackNavigator

const DetailsStack = createNativeStackNavigator();
const DetailsStackNavigator = (): JSX.Element => {
    return (
        <DetailsStack.Navigator
            initialRouteName="UsersScreen"
            screenOptions={{
                headerShown: false
            }}>
            <DetailsStack.Screen
                name="UsersScreen"
                component={UsersScreen}
                options={{ animation: 'none' }}
            />
            <DetailsStack.Screen
                name="OptionsScreen"
                component={OptionsScreen}
                options={{ animation: 'none' }}
            />
            <DetailsStack.Screen name="OptionsDetailsScreen" component={OptionsDetailsScreen} />
            <DetailsStack.Screen name="UsersDetailsScreen" component={UsersDetailsScreen} />
        </DetailsStack.Navigator>
    );
};

我想在optionsdetailsscreen详细信息Stacknavigator内的用户detailsscreen 。我已经尝试了以下几个,但确实隐藏了标签栏,但是它切断了底部的屏幕:

const MainAppBottomTabs = createBottomTabNavigator<BottomTabParamList>();
const MainAppBottomTabsNavigator = (): JSX.Element => {
    const getTabBarVisibility = (route) => {
        const routeName = getFocusedRouteNameFromRoute(route);
        const hideOnScreens = ['OptionsDetailsScreen', 'UsersDetailsScreen'];
        return hideOnScreens.indexOf(routeName) <= -1 ? 'flex' : 'none';
    };
    return (
        <MainAppBottomTabs.Navigator
            // rest of code from above
            <MainAppBottomTabs.Screen
                name="DetailsStackNavigator"
                component={DetailsStackNavigator}
                options={({ route }) => ({
                    tabBarStyle: { display: getTabBarVisibility(route) }
                })}
            />
        </MainAppBottomTabs.Navigator>
    );
};

然后在我的自定义标签栏中:

const TabBar = (screenProps: ScreenProps): JSX.Element => {
    const focusedOptions = screenProps.descriptors[screenProps.state.routes[screenProps.state.index].key].options;

    if (focusedOptions?.tabBarStyle?.display === 'none') {
        return null;
    }
    return <View>{RenderTabs(screenProps)}</View>;
};

不确定要尝试什么。对于我认为这将是一个非常普遍的情况,似乎太困难了。

I want to hide the tab bar on certain screens inside a nested stack navigator. I have a bottom tab bar, using a CUSTOM tab bar, set up like so:

const MainAppBottomTabs = createBottomTabNavigator<BottomTabParamList>();
const MainAppBottomTabsNavigator = (): JSX.Element => {
    return (
        <MainAppBottomTabs.Navigator
            initialRouteName="ListScreen"
            screenOptions={{
                headerShown: false
            }}
            tabBar={(props: BottomTabBarProps) => (
                <TabBar
                    state={props.state}
                    navigation={props.navigation}
                    descriptors={props.descriptors}
                    insets={props.insets}
                /> // this is my custom tab bar
            )}>
            <MainAppBottomTabs.Screen
                name="ListScreen"
                component={ListScreen}
            />
            <MainAppBottomTabs.Screen
                name="ProductsScreen"
                component={ProductsScreen}
            />
            <MainAppBottomTabs.Screen
                name="DetailsStackNavigator"
                component={DetailsStackNavigator}
            />
        </MainAppBottomTabs.Navigator>
    );
};

Here's the nested DetailsStackNavigator:

const DetailsStack = createNativeStackNavigator();
const DetailsStackNavigator = (): JSX.Element => {
    return (
        <DetailsStack.Navigator
            initialRouteName="UsersScreen"
            screenOptions={{
                headerShown: false
            }}>
            <DetailsStack.Screen
                name="UsersScreen"
                component={UsersScreen}
                options={{ animation: 'none' }}
            />
            <DetailsStack.Screen
                name="OptionsScreen"
                component={OptionsScreen}
                options={{ animation: 'none' }}
            />
            <DetailsStack.Screen name="OptionsDetailsScreen" component={OptionsDetailsScreen} />
            <DetailsStack.Screen name="UsersDetailsScreen" component={UsersDetailsScreen} />
        </DetailsStack.Navigator>
    );
};

I want to hide the tab bar on the OptionsDetailsScreen and UsersDetailsScreen inside of the DetailsStackNavigator. I have tried the following which DOES hide the tab bar, but it cuts off the screens at the bottom where the tab bar would be:

const MainAppBottomTabs = createBottomTabNavigator<BottomTabParamList>();
const MainAppBottomTabsNavigator = (): JSX.Element => {
    const getTabBarVisibility = (route) => {
        const routeName = getFocusedRouteNameFromRoute(route);
        const hideOnScreens = ['OptionsDetailsScreen', 'UsersDetailsScreen'];
        return hideOnScreens.indexOf(routeName) <= -1 ? 'flex' : 'none';
    };
    return (
        <MainAppBottomTabs.Navigator
            // rest of code from above
            <MainAppBottomTabs.Screen
                name="DetailsStackNavigator"
                component={DetailsStackNavigator}
                options={({ route }) => ({
                    tabBarStyle: { display: getTabBarVisibility(route) }
                })}
            />
        </MainAppBottomTabs.Navigator>
    );
};

and then in my custom tab bar:

const TabBar = (screenProps: ScreenProps): JSX.Element => {
    const focusedOptions = screenProps.descriptors[screenProps.state.routes[screenProps.state.index].key].options;

    if (focusedOptions?.tabBarStyle?.display === 'none') {
        return null;
    }
    return <View>{RenderTabs(screenProps)}</View>;
};

Not sure what else to try. Seems to be way too difficult for what I would assume would be a pretty common scenario.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文