REACT NAVIGATION August 29, 2018

【React Navigation】十、沉浸式状态栏

Words count 18k Reading time 17 mins. Read count 0

堆栈导航器和抽屉导航器下的沉浸状态栏

在使用堆栈导航器和抽屉导航器下实现沉浸状态栏相对比较简单,你可以直接使用由React Native提供的StatusBar组件,并设置你的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React from 'react';
import {
SafeAreaView,
StatusBar,
Text,
Button,
StyleSheet
} from 'react-native';
import { createStackNavigator, createSwitchNavigator } from 'react-navigation';

class Screen1 extends React.Component {
render() {
return (
<SafeAreaView style={[styles.container, { backgroundColor: '#6a51ae' }]}>
<StatusBar
barStyle="light-content"
backgroundColor="#6a51ae"
/>
<Text style={[styles.paragraph, { color: '#fff' }]}>
Light Screen
</Text>
<Button
title="Next screen"
onPress={() => this.props.navigation.navigate('Screen2')}
color={"blue"}
/>
</SafeAreaView>
);
}
}

class Screen2 extends React.Component {
render() {
return (
<SafeAreaView style={[styles.container, { backgroundColor: '#ecf0f1' }]}>
<StatusBar
barStyle="dark-content"
backgroundColor="#ecf0f1"
/>
<Text style={styles.paragraph}>
Dark Screen
</Text>
<Button
title="Next screen"
onPress={() => this.props.navigation.navigate('Screen1')}
/>
</SafeAreaView>
);
}
}

export default createStackNavigator({
Screen1: {
screen: Screen1,
},
Screen2: {
screen: Screen2,
},
}, {
headerMode: 'none',
});

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ecf0f1',
justifyContent: 'center',
alignItems: 'center',
},
paragraph: {
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
color: '#34495e',
},
});

选项卡导航器下的沉浸式导航器

在选项卡导航器上实现沉浸式状态栏,会相对复杂,因为所有选项卡的页面是同时渲染出来的 - 这意味着你可能只会看到最后一个StatusBar的配置效果。

要解决这个问题,我们必须做两件事:

  • 仅在的初始页面上使用StatusBar组件,这可以确保在初始化时使用了正确的StatusBar配置
  • 利用React Navigation和StatusBar提供的事件,监听在选项卡变为可见时再动态调整StatusBar配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import React from 'react';
import {
SafeAreaView,
StatusBar,
Text,
Button,
StyleSheet,
Platform
} from 'react-native';
import { createStackNavigator, createSwitchNavigator, TabNavigator } from 'react-navigation';

const isAndroid = Platform.OS === 'android';

class Screen1 extends React.Component {

componentDidMount() {
this._navListener = this.props.navigation.addListener('didFocus', () => {
StatusBar.setBarStyle('light-content');
isAndroid && StatusBar.setBackgroundColor('#6a51ae');
});
}

componentWillUnmount() {
this._navListener.remove();
}

render() {
return (
<SafeAreaView style={[styles.container, { backgroundColor: '#6a51ae' }]}>
<StatusBar
barStyle="light-content"
backgroundColor="#6a51ae"
/>
<Text style={[styles.paragraph, { color: '#fff' }]}>
Light Screen
</Text>
<Button
title="Next screen"
onPress={() => this.props.navigation.navigate('Screen2')}
color={"blue"}
/>
</SafeAreaView>
);
}
}

class Screen2 extends React.Component {

componentDidMount() {
this._navListener = this.props.navigation.addListener('didFocus', () => {
StatusBar.setBarStyle('dark-content');
isAndroid && StatusBar.setBackgroundColor('#ecf0f1');
});
}

componentWillUnmount() {
this._navListener.remove();
}

render() {
return (
<SafeAreaView style={[styles.container, { backgroundColor: '#ecf0f1' }]}>
<StatusBar
barStyle="dark-content"
backgroundColor="#ecf0f1"
/>
<Text style={styles.paragraph}>
Dark Screen
</Text>
<Button
title="Next screen"
onPress={() => this.props.navigation.navigate('Screen1')}
/>
</SafeAreaView>
);
}
}

export default TabNavigator({
Screen1: {
screen: Screen1,
navigationOptions: {
tabBarOnPress: ({ previousScene, scene, jumpToIndex }) => {
// TODO: This doesn't handle the initial render because the second screen renders last. What to do?
StatusBar.setBarStyle('light-content');
isAndroid && StatusBar.setBackgroundColor('#6a51ae');
jumpToIndex(scene.index);
},
},
},
Screen2: {
screen: Screen2,
navigationOptions: {
tabBarOnPress: ({ previousScene, scene, jumpToIndex }) => {
StatusBar.setBarStyle('dark-content');
isAndroid && StatusBar.setBackgroundColor('#ecf0f1');
jumpToIndex(scene.index);
},
},
},
});

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ecf0f1',
justifyContent: 'center',
alignItems: 'center',
},
paragraph: {
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
color: '#34495e',
},
});
0%