3 回答

TA貢獻1866條經驗 獲得超5個贊
通過互聯網后,我找到了自己的方法來隱藏特定堆棧屏幕中的底部選項卡。
export default function SignStack({ navigation, route }) {
useEffect(() => {
if (route.state?.index) {
navigation.setOptions({
tabBarVisible: false,
});
} else {
navigation.setOptions({
tabBarVisible: true,
});
}
}, [navigation, route.state?.index]);
return <Stack.Navigator> ... </Stack.Navigator>
}
這將僅顯示第一個堆棧屏幕上的底部選項卡。
更新 Nov 17 2020
使用隱藏底部選項卡,此示例僅在和屏幕上顯示底部選項卡。getFocusedRouteNameFromRouteAuthSettings
const routeName = getFocusedRouteNameFromRoute(route) ?? 'Auth';
useEffect(() => {
navigation.setOptions({
tabBarVisible: ['Auth', 'Settings'].includes(routeName),
});
}, [navigation, routeName]);
為什么不是第一個解決方案斷續器
上面的解決方案根據渲染隱藏屏幕上的底部選項卡 如果您有并行導航路線,則可以使用上述解決方案。route.state.index
假設您有兩個選項卡導航 用戶堆棧和主頁堆棧,在用戶堆棧上,您有兩個屏幕 配置文件和設置,如果要隱藏設置屏幕上的底部欄,您將使用上述工作正常的解決方案 但是當您直接從主頁導航到用戶設置屏幕時,底部選項卡欄顯示在“設置”屏幕上并隱藏在“配置文件”屏幕上, 發生這種情況是因為“設置”屏幕是 和“配置文件”屏幕是 。route.state.index01

TA貢獻1802條經驗 獲得超5個贊
就像你提到的,如果你只希望每個堆棧中的第一個屏幕顯示底部標簽欄,那么我建議你使用第二種方法。創建一個基本堆棧導航器,第一個屏幕是選項卡導航器本身:
const TabScreens = ({navigation}) => { // Tab navigator with only the screens that require bottom tab bar
return (
<Tab.Navigator
initialRouteName="Home"
tabBarOptions={{
activeTintColor: '#e91e63',
}}>
<Tab.Screen
name="Home"
component={Home}
options={{
tabBarLabel: 'Home',
}}
/>
<Tab.Screen
name="Welcome"
component={Welcome}
options={{
tabBarLabel: 'Welcome',
}}
/>
</Tab.Navigator>
);
};
創建選項卡導航器后,在主渲染器中使用:
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Stack"
component={TabScreens}
/>
<Stack.Screen // Add any number of extra screens that do not require the bottom tab here
name="Other screen 1"
component={OtherScreen1} />
<Stack.Screen
name="Other screen 2"
component={OtherScreen2} />
</Stack.Navigator>
</NavigationContainer>
這樣,您就不必擺弄底部選項卡組件。您還可以在基本堆棧導航器的一部分和屬于 Tab 導航器的任何屏幕之間來回導航。這里唯一需要注意的是,每次導航時,除了選項卡導航器中的屏幕之外的所有屏幕都將被安裝和卸載。
添加回答
舉報