我的应用程序的很大一部分由Web视图组成,以提供本机实现尚未提供的功能.网站团队没有为网站实施黑暗主题的计划.因此,我的应用程序在iOS13上支持黑暗模式时,看起来会有点半暗半暗.
有没有可能 Select 不支持黑暗模式,让我们的应用程序始终显示灯光模式,以匹配网站主题?
我的应用程序的很大一部分由Web视图组成,以提供本机实现尚未提供的功能.网站团队没有为网站实施黑暗主题的计划.因此,我的应用程序在iOS13上支持黑暗模式时,看起来会有点半暗半暗.
有没有可能 Select 不支持黑暗模式,让我们的应用程序始终显示灯光模式,以匹配网站主题?
首先,这里有Apple's entry个与 Select 暗模式有关的选项.
在info.plist文件中使用以下密钥:
UIUserInterfaceStyle
并为其赋值Light
.
UIUserInterfaceStyle
个任务的XML:
<key>UIUserInterfaceStyle</key>
<string>Light</string>
Apple documentation for UIUserInterfaceStyle个
你可以根据应用程序的window
变量设置overrideUserInterfaceStyle
.这将应用于窗口中显示的所有视图.这在iOS 13中可用,因此对于支持以前版本的应用程序,必须包含可用性判断.
根据项目的创建方式,这可能在AppDelegate
或SceneDelegate
文件中.
if #available(iOS 13.0, *) {
window?.overrideUserInterfaceStyle = .light
}
您可以针对UIViewController
或UIView
的overrideUserInterfaceStyle
变量设置overrideUserInterfaceStyle
.这在iOS13中已可用,因此对于支持以前版本的应用程序,您必须包括可用性判断.
Swift
override func viewDidLoad() {
super.viewDidLoad()
// overrideUserInterfaceStyle is available with iOS 13
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
overrideUserInterfaceStyle = .light
}
}
为了那些在Objective-C中可怜的灵魂
if (@available(iOS 13.0, *)) {
self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
当针对UIViewController
进行设置时,视图控制器及其子项将采用定义的模式.
当设置为UIView
时,视图及其子视图将采用定义的模式.
Apple documentation for overrideUserInterfaceStyle
可以将preferredColorScheme
设置为light
或dark
.提供的值将设置演示文稿的 colored颜色 方案.
import Swift UI
struct ContentView: View {
var body: some View {
Text("Light Only")
.preferredColorScheme(.light)
}
}
Apple documentation for preferredColorScheme
感谢@Aron Nelson、@Raimundas Sakalauskas、@NSLeader和@rmaddy通过他们的反馈改进了这个答案.