我应该在StackNavigator中的哪里放置“ tabBarOptions”?

时间:2019-12-10 06:50:04

标签: react-native react-native-tabnavigator

我正在使用create-react-native-app,并且正在使用带有createBottomTabNavigator函数的StackNavigator。图标是通过单独的组件进行样式设置的,但是我在设计其余导航器时遇到了麻烦。

我已经创建了要使用的这些选项:

  tabBarOptions: {
    showLabel: false,
    style: {
      backgroundColor: Colors.greyGreen
    }
  }

希望这对某人是很明显的,将不胜感激!我尝试将选项放置在下面的代码内的各个位置(两个位置均不起作用):

const config = Platform.select({
  web: { headerMode: 'screen' },
  default: {},
  // TRIED PLACING THE OPTIONS HERE
});

const HomeStack = createStackNavigator(
  {
    Home: HomeScreen,
  },
  config
);
HomeStack.navigationOptions = {
  tabBarLabel: 'Home',
  tabBarIcon: ({ focused }) => (
    <TabBarIcon
      focused={focused}
      name={
        Platform.OS === 'ios'
          ? `ios-information-circle${focused ? '' : '-outline'}`
          : 'md-information-circle'
      }
    />
  ),
};
HomeStack.path = '';

const LinksStack = createStackNavigator(
  {
    Links: LinksScreen,
  },
  config
);
LinksStack.navigationOptions = {
  tabBarLabel: 'Links',
  tabBarIcon: ({ focused }) => (
    <TabBarIcon focused={focused} name={Platform.OS === 'ios' ? 'ios-link' : 'md-link'} />
  ),
};
LinksStack.path = '';

const SettingsStack = createStackNavigator(
  {
    Settings: SettingsScreen,
  },
  config
);
SettingsStack.navigationOptions = {
  tabBarLabel: 'Settings',
  tabBarIcon: ({ focused }) => (
    <TabBarIcon focused={focused} name={Platform.OS === 'ios' ? 'ios-options' : 'md-options'} />
  ),
};
SettingsStack.path = '';

const tabNavigator = createBottomTabNavigator({
  HomeStack,
  LinksStack,
  SettingsStack,
  // TRIED PLACING THE OPTIONS HERE
});
tabNavigator.path = '';

export default tabNavigator;

如果要查看图标组件:

export default function TabBarIcon(props) {
  return (
    <Ionicons
      name={props.name}
      size={26}
      style={{ marginBottom: -3 }}
      color={props.focused ? Colors.tabIconSelected : Colors.tabIconDefault}
    />
  );
}

2 个答案:

答案 0 :(得分:1)

我相信它已在React Navigation版本5.x中更改。

const Tab = createBottomTabNavigator();

function MyTab() {
    return (
        <Tab.Navigator
            tabBarOptions={{
                showLabel: false
            }}
        >
            <Tab.Screen ... />
            <Tab.Screen ... />
        </Tab.Navigator>
    );
}

答案 1 :(得分:0)

您应该将它放在这里,

const tabNavigator = createBottomTabNavigator({
  HomeStack,
  LinksStack,
  SettingsStack,
},{

tabBarOptions: {
    showLabel: false,
    style: {
      backgroundColor: Colors.greyGreen
    }
  }
});

希望有帮助。毫无疑问