使用 React Native 检测 iPhone iOS 暗模式

joa*_*umg 8 react-native

我知道这可以使用 Swift:

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

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

小智 24

你不需要react-native-dark-mode因为0.62.2。您可以使用

import { Appearance } from 'react-native'
...
Appearance.getColorScheme() === 'dark'
Run Code Online (Sandbox Code Playgroud)

这是添加它的提交:https : //github.com/facebook/react-native/commit/63fa3f21c5ab308def450bffb22054241a8842ef