如何使用React Navigation在React Native中以编程方式隐藏tabbar?

问题描述 投票:0回答:1

我正在使用Create React Native App with Expo来构建应用程序。按下TextInput时,我需要隐藏特定视图中的底部标签栏。 Android默认推送标签栏。

我不想触发标签栏隐藏,因为当键盘未显示时,标签栏必须在视图中。

"expo": "^31.0.2",
"react": "16.5.0",
"react-navigation": "^2.18.2"

我有各种堆栈导出为createBottomTabNavigator。

const SearchStack = createStackNavigator({
  Search: SearchScreen,
  Details: DetailsScreen,
  Tag: TagScreen,
  Category: CategoryScreen,
});

SearchStack.navigationOptions = {
  tabBarLabel: 'Søg',
  tabBarOptions: {
    activeTintColor: Colors.themeColor,
    showLabel: true,
  },
  tabBarIcon: ({ focused }) => (
    <TabBarIcon
      focused={focused}
      name={Platform.OS === 'ios' ? 'ios-search' : 'md-search'}
    />
  ),
};

export default createBottomTabNavigator({
  HomeStack,
  LinksStack,
  InformationStack,
  SearchStack,
});

我可以隐藏导航器中的tabbar,但我希望能够在具有动态navigationOptions / state的特定视图中执行此操作。如果我在屏幕组件中使用tabBarVisible:false它不起作用。

export default class SearchScreen extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      loading: false,
      data: [],
      text: '',
      showClearTextIcon: false,
    };
  }

  static navigationOptions = {
    header: null,
    title: 'Search',
  };

  /**
  * Lifecycle function.
  */
  componentDidMount() {
    this.load()
    this.props.navigation.addListener('willFocus', this.load)
  }

如果在Android上出现键盘或单击按钮时如何隐藏标签栏,您有什么想法吗?

javascript reactjs react-native react-native-android react-navigation
1个回答
0
投票

在屏幕中,您要隐藏标签栏更新导航选项。关键是使animationEnabled成为真,并使用tabBar属性隐藏tabBarVisible

static navigationOptions = ({navigation}) => ({
  tabBarVisible: (navigation.state.params && navigation.state.params.hideTabBar) === true,
  animationEnabled: true
)}

componentWillMount中显示tabBar:

componentWillMount() {
    const setParamsAction = NavigationActions.setParams({
      params: {hideTabBar: true}
    });
    this.props.navigation.dispatch(setParamsAction);
}

componentWillUnmount再次隐藏tabBar:

componentWillUnmount() {
    const setParamsAction = NavigationActions.setParams({
      params: {hideTabBar: false}
    });
    this.props.navigation.dispatch(setParamsAction);
}

您可以检查屏幕的this.statethis.props,以决定何时发生这种情况。

© www.soinside.com 2019 - 2024. All rights reserved.