mirror of
https://github.com/status-im/react-navigation.git
synced 2025-02-25 09:35:19 +00:00
* Initial impl * Fix up some docs and support more for drawer * Fix comments * Support TabBar * Make flow more correct * Clarify even more * Rename all the things lol * Also rename renderLabel to getLabel
5.5 KiB
5.5 KiB
TabNavigator
Used to easily set up a screen with several tabs with a TabRouter.
class MyHomeScreen extends React.Component {
static navigationOptions = {
tabBar: {
label: 'Home',
// Note: By default the icon is only shown on iOS. Search the showIcon option below.
icon: ({ tintColor }) => (
<Image
source={require('./chats-icon.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
},
}
render() {
return (
<Button
onPress={() => this.props.navigation.navigate('Notifications')}
title="Go to notifications"
/>
);
}
}
class MyNotificationsScreen extends React.Component {
static navigationOptions = {
tabBar: {
label: 'Notifications',
icon: ({ tintColor }) => (
<Image
source={require('./notif-icon.png')}
style={[styles.tabIcon, {tintColor: tintColor}]}
/>
),
},
}
render() {
return (
<Button
onPress={() => this.props.navigation.goBack()}
title="Go back home"
/>
);
}
}
const styles = StyleSheet.create({
icon: {
width: 26,
height: 26,
},
});
const MyApp = TabNavigator({
Home: {
screen: MyHomeScreen,
},
Notifications: {
screen: MyNotificationsScreen,
},
}, {
tabBarOptions: {
activeTintColor: '#e91e63',
},
});
API Definition
TabNavigator(RouteConfigs, TabNavigatorConfig)
RouteConfigs
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 from StackNavigator
.
TabNavigatorConfig
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 tabsanimationEnabled
- whether to animate when changing tabslazyLoad
- whether to lazily render tabs as needed as opposed to rendering them upfronttabBarOptions
- configure the tab bar, see below.
Several options get passed to the underlying router to modify navigation logic:
initialRouteName
- The routeName for the initial tab route when first loadingorder
- Array of routeNames which defines the order of the tabspaths
- Provide a mapping of routeName to path config, which overrides the paths set in the routeConfigs.backBehavior
- Should the back button cause a tab switch to the initial tab? If yes, set toinitialRoute
, otherwisenone
. Defaults toinitialRoute
behavior.
tabBarOptions
for TabBarBottom
(default tab bar on iOS)
activeTintColor
- label and icon color of the active tabactiveBackgroundColor
- background color of the active tabinactiveTintColor
- label and icon color of the inactive tabinactiveBackgroundColor
- background color of the inactive tabshowLabel
- whether to show label for tab, default is truestyle
- style object for the tab barlabelStyle
- style object for the tab label
Example:
tabBarOptions: {
activeTintColor: '#e91e63',
labelStyle: { fontSize: 12 },
style: {
backgroundColor: 'blue',
}
}
tabBarOptions
for TabBarTop
(default tab bar on Android)
activeTintColor
- label and icon color of the active tabinactiveTintColor
- label and icon color of the inactive tabshowIcon
- whether to show icon for tab, default is falseshowLabel
- whether to show label for tab, default is trueupperCaseLabel
- whether to make label uppercase, default is truepressColor
- color for material ripple (Android >= 5.0 only)pressOpacity
- opacity for pressed tab (iOS and Android < 5.0 only)scrollEnabled
- whether to enable scrollable tabstabStyle
- style object for the tabindicatorStyle
- style object for the tab indicator (line at the bottom of the tab)labelStyle
- style object for the tab labelstyle
- style object for the tab bar
Example:
tabBarOptions: {
labelStyle: {
fontSize: 12,
},
style: {
backgroundColor: 'blue',
}
}
Screen Navigation Options
Usually you define static navigationOptions
on your screen component. For example:
class ProfileScreen extends React.Component {
static navigationOptions = {
title: ({ state }) => `${state.params.name}'s Profile!`,
tabBar: ({ state, setParams }) => ({
icon: (
<Image src={require('./my-icon.png')} />
),
}),
};
...
All navigationOptions
for the TabNavigator
:
title
- a title (string) of the scenetabBar
- a config object for the tab bar:visible
- Boolean toggle of tab bar visibilityicon
- React Element or a function that given{ focused: boolean, tintColor: string }
returns a React.Element, to display in tab barlabel
- Title string of a tab displayed in the tab bar. When undefined, scenetitle
is used. To hide, seetabBarOptions.showLabel
in the previous section
Navigator Props
The navigator component created by TabNavigator(...)
takes the following props:
screenProps
- Pass down extra options to child screens, for example:
const TabNav = TabNavigator({
// config
});
<TabNav
screenProps={/* this prop will get passed to the screen components as this.props.screenProps */}
/>