我已经使用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>
  );
};

最佳答案

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

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

10-07 12:47
查看更多