如何根据设备主题更改React Native中状态栏的背景?

问题描述 投票:0回答:1

如何根据设备主题实时更改状态栏背景?

我编写了以下代码,我希望每当我切换设备的深色或浅色模式时,状态栏也会发生变化,但我必须从终端重新加载反应本机应用程序才能看到状态栏背景上的变化

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}
      />)

}
react-native statusbar android-darkmode
1个回答
0
投票

尝试

usecolorScheme
挂钩:

import { useColorScheme } from 'react-native';

const colorScheme = useColorScheme()
© www.soinside.com 2019 - 2024. All rights reserved.