使用React Native检测iPhone iOS暗模式

时间:2019-06-29 23:38:06

标签: react-native

我知道可以使用Swift:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if traitCollection.userInterfaceStyle == .light {
        print("Light mode")
    } else {
        print("Dark mode")
    }
}

但是,有没有办法用JavaScript做到这一点?

2 个答案:

答案 0 :(得分:13)

从0.62.2开始,您不需要react-native-dark-mode。 您可以使用

import { Appearance } from 'react-native'
...
Appearance.getColorScheme() === 'dark'

以下是将其添加的提交:https://github.com/facebook/react-native/commit/63fa3f21c5ab308def450bffb22054241a8842ef

答案 1 :(得分:2)

是的...

https://www.npmjs.com/package/react-native-dark-mode

import { useDarkMode } from 'react-native-dark-mode'

function Component() {
  const isDarkMode = useDarkMode()
  return <View style={{ backgroundColor: isDarkMode ? 'black' : 'white' }} />
}