如何根据设备主题实时更改状态栏背景?
我编写了以下代码,我希望每当我切换设备的深色或浅色模式时,状态栏也会发生变化,但我必须从终端重新加载反应本机应用程序才能看到状态栏背景上的变化
const App = () => {
const theme = useTheme();
const colorScheme = Appearance.getColorScheme();
const statusBarBackgroundColor =
colorScheme === 'dark' ? theme.colors.onSurface : theme.colors.surface;
return (
<StatusBar
animated={true}
backgroundColor={statusBarBackgroundColor}
barStyle={colorScheme === 'dark' ? 'light-content' : 'dark-content'}
showHideTransition={statusBarTransition}
hidden={hidden}
/>)
}
尝试
usecolorScheme
挂钩:
import { useColorScheme } from 'react-native';
const colorScheme = useColorScheme()