如何在反应本机中将选项卡栏中的选项卡垂直居中



我使用 https://reactnavigation.org/docs/en/bottom-tab-navigator.html 中的 createBottomTabNavigator 在 react native 中创建了一个导航器

遇到的问题是我找不到将选项卡栏内的选项卡垂直居中的方法。

正如您在屏幕截图中看到的那样,选项卡底部始终存在蓝色区域。即使我手动设置选项卡的高度,它们也会向上生长。如果我为选项卡栏设置flex:1,它会占用屏幕的一半,但蓝色区域仍然存在。

tabBar: {
  backgroundColor: 'blue',
  borderWidth: 2,
  height: 32,
  justifyContent: 'center',
  alignItems: 'center',
  padding: 0
},
labelStyle: {
  backgroundColor: 'green',
},
tabStyle: {
  backgroundColor: 'yellow',
  flex: 1,
  justifyContent: 'center',
  alignItems: 'center',
  alignSelf: 'center',
  borderWidth: 1,
  borderColor: 'black',
  marginBottom: 0,
  paddingBottom: 0,
},

这就是我创建导航栏的方式(为简单起见,我删除了图标):

const TabNavigator = createBottomTabNavigator(
  {
    screen1: { screen: screen1 },
    screen2: { screen: screen2 },
    screen3: { screen: screen3 },
    screen4: { screen: screen4 },
  },
  {
    tabBarOptions: {
      style: styles.tabBar,
      labelStyle: styles.labelStyle,
      tabStyle: styles.tabStyle
    },
  }
);
const App = createAppContainer(TabNavigator);
export default () => { 
  return (
    <SafeAreaView style={{ flex: 1, backgroundColor: 'red' }}>
      <App />
    </SafeAreaView>
  );
};

我自己找到了解决方案,我正在为有同样问题的人分享。底部间距始终存在的原因是一个名为 safeAreaInset 的 prop,其默认值为 { bottom: 'always', top: 'never' }

我所要做的就是将bottom的值更改为never,它不会在底部增加任何间距!

这是

由于标签上方存在图标组件。为了隐藏图标组件,我添加了以下代码。

tabBarOptions: {
  tabStyle: {
    justifyContent: 'center'
  },
  showIcon: false
} 

尝试为 v 6.x

tabBarStyle:{ paddingBottom: 0 }

如果未显示图标,请在标签样式中添加{position: 'absolute', textAlignVertical: 'center'},例如:

<Tab.Navigator
  screenOptions={{
      tabBarIconStyle: {display: 'none'},
      tabBarStyle: {
        height: 40,
      },
      tabBarLabelStyle: {
        fontSize: 20,
        position: 'absolute',
        textAlignVertical: 'center',
      },  
}}>
我认为您应该将

选项卡栏包装在视图中并在那里添加理由内容

最新更新