Skip to content

Commit 313ec7a

Browse files
davepackspencercarli
authored andcommitted
Fix backgroundColor in Header and TabBar (react-navigation#2871)
* Add headerBackgroundColor to navigationOptions * Add backgroundColor to tabBarOptions * Update snapshots. * Update docs.
1 parent ed2fc9a commit 313ec7a

File tree

8 files changed

+50
-26
lines changed

8 files changed

+50
-26
lines changed

docs/api/navigators/StackNavigator.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ Style object for the header
135135

136136
Style object for the title component
137137

138+
### `headerBackgroundColor`
139+
140+
Color string that overrides default background color.
141+
138142
#### `headerBackTitleStyle`
139143

140144
Style object for the back title

docs/api/navigators/TabNavigator.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Several options get passed to the underlying router to modify navigation logic:
106106
- `inactiveBackgroundColor` - Background color of the inactive tab.
107107
- `showLabel` - Whether to show label for tab, default is true.
108108
- `style` - Style object for the tab bar.
109+
- `backgroundColor` - Color string that overrides default backgroundColor.
109110
- `labelStyle` - Style object for the tab label.
110111
- `tabStyle` - Style object for the tab.
111112
- `allowFontScaling` - Whether label font should scale to respect Text Size accessibility settings, default is true.

src/TypeDefinition.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ export type NavigationStackScreenOptions = {|
360360
headerPressColorAndroid?: string,
361361
headerRight?: React.Node,
362362
headerStyle?: ViewStyleProp,
363+
headerBackgroundColor?: string,
363364
gesturesEnabled?: boolean,
364365
gestureResponseDistance?: { vertical?: number, horizontal?: number },
365366
|};

src/navigators/__tests__/__snapshots__/StackNavigator-test.js.snap

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,15 @@ exports[`StackNavigator applies correct values when headerRight is present 1`] =
7878
onLayout={[Function]}
7979
style={
8080
Array [
81-
Object {
82-
"backgroundColor": "#F7F7F7",
83-
"borderBottomColor": "rgba(0, 0, 0, .3)",
84-
"borderBottomWidth": 0.5,
85-
},
81+
Array [
82+
Object {
83+
"borderBottomColor": "rgba(0, 0, 0, .3)",
84+
"borderBottomWidth": 0.5,
85+
},
86+
Object {
87+
"backgroundColor": "#F7F7F7",
88+
},
89+
],
8690
Object {
8791
"paddingBottom": 0,
8892
"paddingLeft": 0,
@@ -314,11 +318,15 @@ exports[`StackNavigator renders successfully 1`] = `
314318
onLayout={[Function]}
315319
style={
316320
Array [
317-
Object {
318-
"backgroundColor": "#F7F7F7",
319-
"borderBottomColor": "rgba(0, 0, 0, .3)",
320-
"borderBottomWidth": 0.5,
321-
},
321+
Array [
322+
Object {
323+
"borderBottomColor": "rgba(0, 0, 0, .3)",
324+
"borderBottomWidth": 0.5,
325+
},
326+
Object {
327+
"backgroundColor": "#F7F7F7",
328+
},
329+
],
322330
Object {
323331
"paddingBottom": 0,
324332
"paddingLeft": 0,

src/navigators/__tests__/__snapshots__/TabNavigator-test.js.snap

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ exports[`TabNavigator renders successfully 1`] = `
6868
onLayout={[Function]}
6969
style={
7070
Array [
71-
Object {
72-
"backgroundColor": "#F7F7F7",
73-
"borderTopColor": "rgba(0, 0, 0, .3)",
74-
"borderTopWidth": 0.5,
75-
},
71+
Array [
72+
Object {
73+
"borderTopColor": "rgba(0, 0, 0, .3)",
74+
"borderTopWidth": 0.5,
75+
},
76+
Object {
77+
"backgroundColor": "#F7F7F7",
78+
},
79+
],
7680
Object {
7781
"paddingBottom": 0,
7882
"paddingLeft": 0,

src/views/Header/Header.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ class Header extends React.PureComponent<Props, State> {
302302
} = this.props;
303303

304304
const { options } = this.props.getScreenDetails(scene);
305-
const headerStyle = options.headerStyle;
305+
const {
306+
headerStyle,
307+
headerBackgroundColor = Platform.OS === 'ios' ? '#F7F7F7' : '#FFF',
308+
} = options;
306309
const appBarHeight = Platform.OS === 'ios' ? (isLandscape ? 32 : 44) : 56;
307310
const containerStyles = [
308311
{
@@ -313,7 +316,7 @@ class Header extends React.PureComponent<Props, State> {
313316

314317
return (
315318
<SafeAreaView
316-
style={styles.container}
319+
style={[styles.container, { backgroundColor: headerBackgroundColor }]}
317320
forceInset={{ top: 'always', bottom: 'never' }}
318321
>
319322
<Animated.View {...rest} style={containerStyles}>
@@ -344,7 +347,6 @@ if (Platform.OS === 'ios') {
344347

345348
const styles = StyleSheet.create({
346349
container: {
347-
backgroundColor: Platform.OS === 'ios' ? '#F7F7F7' : '#FFF',
348350
...platformContainerStyles,
349351
},
350352
appBar: {

src/views/TabView/TabBarBottom.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Props = {
4040
getTestIDProps: (scene: TabScene) => (scene: TabScene) => any,
4141
renderIcon: (scene: TabScene) => React.Node,
4242
style?: ViewStyleProp,
43+
backgroundColor?: string,
4344
labelStyle?: TextStyleProp,
4445
tabStyle?: ViewStyleProp,
4546
showIcon?: boolean,
@@ -164,6 +165,7 @@ class TabBarBottom extends React.PureComponent<Props> {
164165
inactiveBackgroundColor,
165166
style,
166167
tabStyle,
168+
backgroundColor = '#F7F7F7', // Default background color in iOS 10
167169
isLandscape,
168170
} = this.props;
169171
const { routes } = navigation.state;
@@ -180,7 +182,7 @@ class TabBarBottom extends React.PureComponent<Props> {
180182

181183
return (
182184
<SafeAreaView
183-
style={styles.tabBarContainer}
185+
style={[styles.tabBarContainer, { backgroundColor }]}
184186
forceInset={{ bottom: 'always' }}
185187
>
186188
<Animated.View style={tabBarStyle}>
@@ -236,12 +238,10 @@ const LABEL_LEFT_MARGIN = 20;
236238
const LABEL_TOP_MARGIN = 15;
237239
const styles = StyleSheet.create({
238240
tabBarContainer: {
239-
backgroundColor: '#F7F7F7', // Default background color in iOS 10
240241
borderTopWidth: StyleSheet.hairlineWidth,
241242
borderTopColor: 'rgba(0, 0, 0, .3)',
242243
},
243244
tabBar: {
244-
// height: 49, // Default tab bar height in iOS 10+
245245
flexDirection: 'row',
246246
},
247247
tabBarLandscape: {

src/views/__tests__/__snapshots__/TabView-test.js.snap

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ exports[`TabBarBottom renders successfully 1`] = `
2424
onLayout={[Function]}
2525
style={
2626
Array [
27-
Object {
28-
"backgroundColor": "#F7F7F7",
29-
"borderTopColor": "rgba(0, 0, 0, .3)",
30-
"borderTopWidth": 0.5,
31-
},
27+
Array [
28+
Object {
29+
"borderTopColor": "rgba(0, 0, 0, .3)",
30+
"borderTopWidth": 0.5,
31+
},
32+
Object {
33+
"backgroundColor": "#F7F7F7",
34+
},
35+
],
3236
Object {
3337
"paddingBottom": 0,
3438
"paddingLeft": 0,

0 commit comments

Comments
 (0)