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

时间:2019-01-09 17:58:05

标签: javascript react-native flexbox react-navigation tabnavigator

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

我遇到的问题是我找不到在选项卡栏内垂直放置选项卡的方法。

如您在screenshot中所见,选项卡底部始终有蓝色区域。即使当我手动设置标签的高度时,它们也会向上增长。如果我将标签栏设置为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>
  );
};

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

tabBarOptions: {
  tabStyle: {
    justifyContent: 'center'
  },
  showIcon: false
} 
相关问题