2017-01-26 11:49:39 -08:00
# TabNavigator
Used to easily set up a screen with several tabs with a TabRouter.
```js
class MyHomeScreen extends React.Component {
static navigationOptions = {
2017-04-13 00:49:08 +02:00
tabBarLabel: 'Home',
// Note: By default the icon is only shown on iOS. Search the showIcon option below.
tabBarIcon: ({ tintColor }) => (
< Image
source={require('./chats-icon.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
};
2017-01-26 11:49:39 -08:00
render() {
return (
< Button
onPress={() => this.props.navigation.navigate('Notifications')}
2017-02-05 21:59:11 -05:00
title="Go to notifications"
2017-01-26 11:49:39 -08:00
/>
);
}
}
class MyNotificationsScreen extends React.Component {
static navigationOptions = {
2017-04-13 00:49:08 +02:00
tabBarLabel: 'Notifications',
tabBarIcon: ({ tintColor }) => (
< Image
source={require('./notif-icon.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
};
2017-01-26 11:49:39 -08:00
render() {
return (
< Button
onPress={() => this.props.navigation.goBack()}
2017-02-05 21:59:11 -05:00
title="Go back home"
2017-01-26 11:49:39 -08:00
/>
);
}
}
const styles = StyleSheet.create({
icon: {
width: 26,
height: 26,
},
});
const MyApp = TabNavigator({
Home: {
screen: MyHomeScreen,
},
Notifications: {
screen: MyNotificationsScreen,
},
}, {
tabBarOptions: {
activeTintColor: '#e91e63 ',
},
});
```
2017-02-02 05:27:05 +08:00
## API Definition
```js
TabNavigator(RouteConfigs, TabNavigatorConfig)
```
### RouteConfigs
2017-03-22 15:51:16 -05:00
The route configs object is a mapping from route name to a route config, which tells the navigator what to present for that route, see [example ](/docs/api/navigators/StackNavigator.md#routeconfigs ) from `StackNavigator` .
2017-02-02 05:27:05 +08:00
### TabNavigatorConfig
2017-01-26 11:49:39 -08:00
- `tabBarComponent` - component to use as the tab bar, e.g. `TabView.TabBarBottom`
(this is the default on iOS), `TabView.TabBarTop`
(this is the default on Android)
- `tabBarPosition` - position of the tab bar, can be `'top'` or `'bottom'`
- `swipeEnabled` - whether to allow swiping between tabs
- `animationEnabled` - whether to animate when changing tabs
- `lazyLoad` - whether to lazily render tabs as needed as opposed to rendering them upfront
- `tabBarOptions` - configure the tab bar, see below.
Several options get passed to the underlying router to modify navigation logic:
2017-02-01 03:26:42 +01:00
- `initialRouteName` - The routeName for the initial tab route when first loading
2017-01-26 11:49:39 -08:00
- `order` - Array of routeNames which defines the order of the tabs
- `paths` - Provide a mapping of routeName to path config, which overrides the paths set in the routeConfigs.
2017-02-01 03:26:42 +01:00
- `backBehavior` - Should the back button cause a tab switch to the initial tab? If yes, set to `initialRoute` , otherwise `none` . Defaults to `initialRoute` behavior.
2017-01-26 11:49:39 -08:00
### `tabBarOptions` for `TabBarBottom` (default tab bar on iOS)
- `activeTintColor` - label and icon color of the active tab
- `activeBackgroundColor` - background color of the active tab
- `inactiveTintColor` - label and icon color of the inactive tab
- `inactiveBackgroundColor` - background color of the inactive tab
2017-02-08 08:54:02 -08:00
- `showLabel` - whether to show label for tab, default is true
2017-01-26 11:49:39 -08:00
- `style` - style object for the tab bar
2017-02-04 13:33:38 +01:00
- `labelStyle` - style object for the tab label
2017-01-26 11:49:39 -08:00
Example:
```js
tabBarOptions: {
activeTintColor: '#e91e63 ',
2017-03-07 01:50:12 -05:00
labelStyle: {
fontSize: 12,
},
2017-01-26 11:49:39 -08:00
style: {
backgroundColor: 'blue',
2017-03-07 01:50:12 -05:00
},
2017-01-26 11:49:39 -08:00
}
```
### `tabBarOptions` for `TabBarTop` (default tab bar on Android)
- `activeTintColor` - label and icon color of the active tab
- `inactiveTintColor` - label and icon color of the inactive tab
- `showIcon` - whether to show icon for tab, default is false
- `showLabel` - whether to show label for tab, default is true
- `upperCaseLabel` - whether to make label uppercase, default is true
- `pressColor` - color for material ripple (Android >= 5.0 only)
- `pressOpacity` - opacity for pressed tab (iOS and Android < 5.0 only )
- `scrollEnabled` - whether to enable scrollable tabs
- `tabStyle` - style object for the tab
- `indicatorStyle` - style object for the tab indicator (line at the bottom of the tab)
- `labelStyle` - style object for the tab label
2017-04-08 20:02:52 +02:00
- `iconStyle` - style object for the tab icon
2017-01-26 11:49:39 -08:00
- `style` - style object for the tab bar
Example:
```js
tabBarOptions: {
labelStyle: {
fontSize: 12,
},
style: {
backgroundColor: 'blue',
2017-03-07 01:50:12 -05:00
},
2017-01-26 11:49:39 -08:00
}
```
2017-02-23 16:09:11 +01:00
### Screen Navigation Options
2017-04-13 00:49:08 +02:00
#### `title`
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
Generic title that can be used as a fallback for `headerTitle` and `tabBarLabel`
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
#### `tabBarVisible`
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
True or false to show or hide the tab bar, if not set then defaults to true
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
#### `tabBarIcon`
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
React Element or a function that given `{ focused: boolean, tintColor: string }` returns a React.Element, to display in tab bar
#### `tabBarLabel`
2017-04-22 08:11:45 +02:00
Title string of a tab displayed in the tab bar or React Element or a function that given `{ focused: boolean, tintColor: string }` returns a React.Element, to display in tab bar. When undefined, scene `title` is used. To hide, see `tabBarOptions.showLabel` in the previous section.
2017-02-23 16:09:11 +01:00
2017-01-26 11:49:39 -08:00
### Navigator Props
2017-02-02 05:27:05 +08:00
The navigator component created by `TabNavigator(...)` takes the following props:
2017-04-13 00:49:08 +02:00
- `screenProps` - Pass down extra options to child screens and navigation options, for example:
2017-02-02 05:27:05 +08:00
2017-01-26 11:49:39 -08:00
2017-02-02 05:27:05 +08:00
```jsx
const TabNav = TabNavigator({
// config
});
2017-04-13 00:49:08 +02:00
2017-02-02 05:27:05 +08:00
< TabNav
2017-02-09 00:30:09 +05:30
screenProps={/* this prop will get passed to the screen components as this.props.screenProps */}
2017-02-02 05:27:05 +08:00
/>
```