我的应用程序的很大一部分由web视图组成,以提供尚未通过本机实现提供的功能。网络团队没有计划为网站实施黑暗主题。因此,我的应用程序在iOS 13上的暗模式支持看起来会有点一半一半。
是否可以选择退出暗模式支持,这样我们的应用程序总是显示光模式,以匹配网站主题?
我的应用程序的很大一部分由web视图组成,以提供尚未通过本机实现提供的功能。网络团队没有计划为网站实施黑暗主题。因此,我的应用程序在iOS 13上的暗模式支持看起来会有点一半一半。
是否可以选择退出暗模式支持,这样我们的应用程序总是显示光模式,以匹配网站主题?
当前回答
我将使用这个解决方案,因为窗口属性可能会在应用程序生命周期中发生变化。因此需要重复分配" overrideuserinterfacstyle = .light"。UIWindow.appearance()使我们能够为新创建的UIWindow对象设置默认值。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 13.0, *) {
UIWindow.appearance().overrideUserInterfaceStyle = .light
}
return true
}
}
其他回答
在ViewController.swift文件中添加overrideUserInterfaceStyle = .light或在info中更改Appearance为“light”。plist文件
这个问题有很多答案,而不是在信息中使用它。plist你可以在AppDelegate中这样设置:
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
self.window?.overrideUserInterfaceStyle = .light
}
#endif
在Xcode 11.3和iOS 13.3上测试
我想我找到解决办法了。我最初从UIUserInterfaceStyle -信息属性列表和UIUserInterfaceStyle - UIKit拼凑在一起,但现在发现它实际上记录在为你的iOS应用程序选择特定的界面风格。
在你的信息里。将UIUserInterfaceStyle (User InterfaceStyle)设置为1 (UIUserInterfaceStyle.light)。
编辑:根据dorbeetle的回答,uiuserinterfacstyle更合适的设置可能是Light。
根据苹果关于“在iOS上实现暗模式”(https://developer.apple.com/videos/play/wwdc2019/214/在31:13开始)的会话,可以在任何视图控制器或视图上设置overrideUserInterfaceStyle为UIUserInterfaceStyleLight或UIUserInterfaceStyleDark,这将被用于任何子视图或视图控制器的traitCollection中。
正如SeanR所提到的,你可以在应用程序的plist文件中设置uiuserinterfacstyle为Light或Dark来改变整个应用程序。
斯威夫特5
您可以通过以下链接下载演示项目:- https://github.com/rashidlatif55/DarkLightMode
两种方法切换暗到亮模式:
1- info.plist
<key>UIUserInterfaceStyle</key>
<string>Light</string>
2-程序化或运行时
@IBAction private func switchToDark(_ sender: UIButton){
UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .dark
}