根據路徑的不同狀態欄位設定
如果你沒有導覽標頭,或導覽標頭會根據路線更換顏色,你可能希望確保內容使用正確的顏色。
堆疊
當使用堆疊時,這是個簡單的任務。你可以呈現由 React Native 公開的 StatusBar
組件,並設定你的組態檔。
import * as React from 'react';
import { View, Text, StatusBar, Button, StyleSheet } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import {
SafeAreaProvider,
useSafeAreaInsets,
} from 'react-native-safe-area-context';
function Screen1({ navigation }) {
const insets = useSafeAreaInsets();
return (
<View
style={[
styles.container,
{
backgroundColor: '#6a51ae',
paddingTop: insets.top,
paddingBottom: insets.bottom,
paddingLeft: insets.left,
paddingRight: insets.right,
},
]}
>
<StatusBar barStyle="light-content" backgroundColor="#6a51ae" />
<Text style={{ color: '#fff' }}>Light Screen</Text>
<Button
title="Next screen"
onPress={() => navigation.navigate('Screen2')}
color="#fff"
/>
</View>
);
}
function Screen2({ navigation }) {
const insets = useSafeAreaInsets();
return (
<View
style={[
styles.container,
{
backgroundColor: '#ecf0f1',
paddingTop: insets.top,
paddingBottom: insets.bottom,
paddingLeft: insets.left,
paddingRight: insets.right,
},
]}
>
<StatusBar barStyle="dark-content" backgroundColor="#ecf0f1" />
<Text>Dark Screen</Text>
<Button
title="Next screen"
onPress={() => navigation.navigate('Screen1')}
/>
</View>
);
}
const Stack = createNativeStackNavigator();
export default function App() {
return (
<SafeAreaProvider>
<NavigationContainer>
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="Screen1" component={Screen1} />
<Stack.Screen name="Screen2" component={Screen2} />
</Stack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
標籤和抽屜
如果你使用的是標籤或抽屜導覽器,情況就會稍微複雜一點,因為導覽器中的所有畫面可能同時呈現並維持呈現狀態 - 這表示所設定的最後一個 StatusBar
組態檔會被使用(可能出現在標籤導覽器的最後一個標籤上,而非使用者所看到的地方)。
要修正這個問題,我們必須使狀態欄組件感知螢幕焦點,並僅在螢幕取得焦點時才進行渲染。我們可以使用 useIsFocused
繫鉤 和建立包覆組件來達成此目的
import * as React from 'react';
import { StatusBar } from 'react-native';
import { useIsFocused } from '@react-navigation/native';
function FocusAwareStatusBar(props) {
const isFocused = useIsFocused();
return isFocused ? <StatusBar {...props} /> : null;
}
現在,我們的螢幕(Screen1.js
和 Screen2.js
)將使用 FocusAwareStatusBar
組件,而不是 React Native 中的 StatusBar
組件
function Screen1({ navigation }) {
const insets = useSafeAreaInsets();
return (
<View
style={[
styles.container,
{
backgroundColor: '#6a51ae',
paddingTop: insets.top,
paddingBottom: insets.bottom,
paddingLeft: insets.left,
paddingRight: insets.right,
},
]}
>
<FocusAwareStatusBar barStyle="light-content" backgroundColor="#6a51ae" />
<Text style={{ color: '#fff' }}>Light Screen</Text>
<Button
title="Next screen"
onPress={() => navigation.navigate('Screen2')}
color="#fff"
/>
</View>
);
}
function Screen2({ navigation }) {
const insets = useSafeAreaInsets();
return (
<View
style={[
styles.container,
{
backgroundColor: '#ecf0f1',
paddingTop: insets.top,
paddingBottom: insets.bottom,
paddingLeft: insets.left,
paddingRight: insets.right,
},
]}
>
<FocusAwareStatusBar barStyle="dark-content" backgroundColor="#ecf0f1" />
<Text>Dark Screen</Text>
<Button
title="Next screen"
onPress={() => navigation.navigate('Screen1')}
/>
</View>
);
}
儘管沒有必要,您也可以在原生堆疊導航器的螢幕中使用 FocusAwareStatusBar
組件。