如何对TabNavigator隐藏特定的TabBar项。是否有特定的TabBarOptions选项,其visible键(true/false)如下所示?
const Tabs = TabNavigator({
Home: {
screen: Home
},
Profile: {
screen: Thanks,
tabBarOptions: {
visible: false
},
},
More: {
screen: More
},
})发布于 2018-05-13 07:48:39
对于在TabNavigator中隐藏特定项目,没有“可见”选项。
您需要创建Stacknavigator和Tabnavigator。在Stacknavigator中,您将添加您的“不可见”选项卡项,并在末尾添加其“可见”选项卡项的Tabnavigator。
const TabsScreen = TabNavigator({
Profile: { // visible TabBar item
screen: Thanks,
tabBarOptions: {
visible: false
},
},
More: { // visible TabBar item
screen: More
},
})
const MainScreenNavigator = StackNavigator({
Home: { // invisible TabBar item
screen: Home,
navigationOptions : {
header: null /* hide header*/
}
},
Screen 2: { }, // invisible TabBar item
Screen 3: { }, // invisible TabBar item
Screen 4: { }, // invisible TabBar item
TabsScreen: {
screen: TabsScreen,
navigationOptions : {
header: null /* hide header*/
}
},
});发布于 2019-05-15 06:41:00
tabBarOptions的问题是只隐藏所选屏幕的当前导航(选项卡)。但不隐藏/显示选项卡。
tabBarOptions: {
visible: false
}自定义解决方案
我使用createMaterialTopTabNavigator创建了一些特殊的类来实现这一点
export default class CustomTabsNavigator extends Component {
// Final navigation setup with screens
TabsNavigation;
constructor(props) {
super(props);
// Only this is necessary if you just want to hide/show without change it.
this._setScreens();
}
// This is necessary if you want to hide/show depending on some user behavior
componentDidUpdate(prevProps) {
// Add your condition to avoid infinite renders
if (prevProps.foo === this.props.foo) return;
this._setScreens();
}
// Actual code to show/hide based on props.
_setScreens = () => {
const { isAdmin } = this.props;
const screens = {};
const settings = {
tabBarOptions: {
style: {...}
}
};
// Set tabs depending on user and state
if (isAdmin) {
screens.Dashboard = {
screen: DashboardScreen,
navigationOptions: { ... }
};
}
screens.Home = { screen: HomeScreen };
this.TabsNavigation = createMaterialTopTabNavigator(screens, settings);
// Since we are not using setState
this.forceUpdate();
};
render() {
// AppContainer is required in react-native version 3.x
const { props } = this;
const NavigatorTabs = createAppContainer(this.TabsNavigation);
return <NavigatorTabs screenProps={{ ...props }} />;
}
}tabs的实现:
<CustomTabsNavigator isAdmin={true} />发布于 2021-06-11 10:32:39
我的解决方案是返回一个自定义选项卡栏按钮,即:高度和宽度设置为0的视图,工作正常
<Tabs.Screen name="Refer A Friend" component={WebViewComponent}
options={{
tabBarButton: () => (
<View style={{width:0, height:0}}></View>
),
tabBarVisible:false //hide tab bar on this screen
}}
/>更新:正如@Aman Deep所指出的,你可以只返回null
<Tabs.Screen name="Refer A Friend" component={WebViewComponent}
options={{
tabBarButton: () => null,
tabBarVisible:false //hide tab bar on this screen
}}
/>https://stackoverflow.com/questions/47954189
复制相似问题