在 TabNavigator 中隐藏 TabBar 项目

IT技术 reactjs react-native react-navigation tabnavigator stack-navigator
2021-05-04 23:27:06

如何从 TabNavigator 隐藏某些 TabBar 项目。是否有某个TabBarOptions选项,其中有这样的visible键(真/假)?

const Tabs = TabNavigator({
  Home: {
    screen: Home
  },
  Profile: {
    screen: Thanks,
    tabBarOptions: {
      visible: false
    },
  },
  More: {
    screen: More
  },
})
4个回答

没有用于从 TabNavigator 隐藏特定项目的“可见”选项。

您需要创建一个 Stacknavigator 和一个 Tabnavigator。在 Stacknavigator 中,您将添加您的“不可见”标签栏项目,并在最后添加“可见”标签栏项目的 Tabnavigator。

作者:@ragnorc来自GitHub

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*/
        }
    },
});

问题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 }} />;
  }
}

选项卡的实现:

<CustomTabsNavigator isAdmin={true} />

我的解决方案是返回一个自定义 tabbarbutton,它是:高度和宽度设置为 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

    }}
/>

单个选项卡没有这样的“可见”选项,尽管有人讨论过它的实现

可以仅呈现某些选项卡。您可以通过向 TabNavigator 传递您希望在特定时间显示的特定选项卡来动态呈现您的 TabNavigator。您无需将参数硬编码到 TabNavigator(),而是将参数设置为代表您要呈现的选项卡的对象,然后您可以随着时间的推移对该对象进行更改。

有关的巧妙实现,请参见此处