简体   繁体   English

如何在 React Native 中垂直居中标签栏中的标签

[英]How to vertically centre tabs inside tab bar in react native

I've created a navigator in react native using createBottomTabNavigator from https://reactnavigation.org/docs/en/bottom-tab-navigator.html我已经使用https://reactnavigation.org/docs/en/bottom-tab-navigator.html 中的createBottomTabNavigator在本机反应中创建了一个导航器

The problem I'm having is that I can't find a way to vertically centre the tabs inside the tab bar.我遇到的问题是我找不到将标签栏内的标签垂直居中的方法。

As you can see in the screenshot there is always that blue area at the bottom of the tabs.正如您在屏幕截图中看到的,选项卡底部总是有蓝色区域。 Even when I manually set the height for the tabs, they grow upward.即使我手动设置选项卡的高度,它们也会向上生长。 If I set flex:1 for the tab bar, it takes half of the screen, but the blue area still exists.如果我为标签栏设置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,
},

and this is how I create the nav bar (I removed the icons for simplicity):这就是我创建导航栏的方式(为简单起见,我删除了图标):

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>
  );
};

This is due to icon component present above label. 这是由于标签上方存在图标组件。 To hide icon component i added follow code. 为了隐藏图标组件,我添加了以下代码。

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

我认为您应该将标签栏包装在视图中并在其中添加justifyContent

I found the solution myself and I'm sharing it for people who have the same problem. 我自己找到了解决方案,并与有相同问题的人分享。 The reason the bottom spacing is always there is because of a prop called safeAreaInset and its default value is { bottom: 'always', top: 'never' } 底部间距始终存在的原因是因为有一个名为safeAreaInset ,其默认值为{ bottom: 'always', top: 'never' }

All I had to do was to change the value for bottom to never and it won't add any spacing to the bottom! 我要做的就是将bottom的值更改为never ,并且不会在底部增加任何间距!

尝试将 v 6.x

tabBarStyle:{ paddingBottom: 0 }

If you not showing icon add {position: 'absolute', textAlignVertical: 'center'} in label style, example:如果不显示图标,请在标签样式中添加 {position: 'absolute', textAlignVertical: 'center'},例如:

<Tab.Navigator
  screenOptions={{
      tabBarIconStyle: {display: 'none'},
      tabBarStyle: {
        height: 40,
      },
      tabBarLabelStyle: {
        fontSize: 20,
        position: 'absolute',
        textAlignVertical: 'center',
      },  

}}> }}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM