是否可以在 iOS 13 上选择退出暗模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56537855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to opt-out of dark mode on iOS 13?
提问by SeanR
A large part of my app consists of web views to provide functionality not yet available through native implementations. The web team has no plans to implement a dark theme for the website. As such, my app will look a bit half/half with Dark Mode support on iOS 13.
我的应用程序的很大一部分由 Web 视图组成,以提供本机实现尚不可用的功能。网络团队没有计划为网站实施深色主题。因此,我的应用程序在 iOS 13 上支持深色模式时看起来有点半/半。
Is it possible to opt out of Dark Mode support such that our app always shows light mode to match the website theme?
是否可以选择退出暗模式支持,以便我们的应用始终显示亮模式以匹配网站主题?
回答by CodeBender
First, here is Apple's entryrelated to opting out of dark mode. The content at this link is written for Xcode 11 & iOS 13:
首先,这是Apple与选择退出黑暗模式相关的条目。 此链接中的内容是为 Xcode 11 和 iOS 13 编写的:
This section applies to Xcode 11 usage
本节适用于 Xcode 11 使用
If you wish to opt out your ENTIRE application
如果您想退出整个应用程序
Approach #1
方法#1
Use the following key in your info.plistfile:
在 info.plist文件中使用以下键:
UIUserInterfaceStyle
And assign it a value of Light
.
并为其赋值Light
。
The XMLfor the UIUserInterfaceStyle
assignment:
该XML的UIUserInterfaceStyle
分配:
<key>UIUserInterfaceStyle</key>
<string>Light</string>
Approach #2
方法#2
You can set overrideUserInterfaceStyle
against the app's window
variable.
您可以overrideUserInterfaceStyle
针对应用程序的window
变量进行设置。
Depending on how your project was created, this may be in the AppDelegate
file or the SceneDelegate
.
根据项目的创建方式,这可能在AppDelegate
文件或SceneDelegate
.
if #available(iOS 13.0, *) {
window?.overrideUserInterfaceStyle = .light
}
If you wish to opt out your UIViewController on an individual basis
如果您希望单独选择退出 UIViewController
override func viewDidLoad() {
super.viewDidLoad()
// overrideUserInterfaceStyle is available with iOS 13
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
overrideUserInterfaceStyle = .light
}
}
Apple documentation for overrideUserInterfaceStyle
关于 overrideUserInterfaceStyle 的 Apple 文档
How the above code will look in Xcode 11:
上面的代码在 Xcode 11 中的样子:
This section applies to Xcode 10.x usage
本节适用于 Xcode 10.x 使用
If you are using Xcode 11 for your submission, you can safely ignore everything below this line.
如果您使用 Xcode 11 进行提交,则可以放心地忽略此行下方的所有内容。
Since the relevant API does not exist in iOS 12, you will get errors when attempting to use the values provided above:
由于 iOS 12 中不存在相关 API,因此在尝试使用上面提供的值时会出现错误:
For setting overrideUserInterfaceStyle
in your UIViewController
用于设置overrideUserInterfaceStyle
您的UIViewController
If you wish to opt out your UIViewController on an individual basis
如果您希望单独选择退出 UIViewController
This can be handled in Xcode 10 by testing the compiler version and the iOS version:
这可以在 Xcode 10 中通过测试编译器版本和 iOS 版本来处理:
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
overrideUserInterfaceStyle = .light
}
#endif
If you wish to opt out your ENTIRE application
如果您想退出整个应用程序
You can modify the above snippet to work against the entire application for Xcode 10, by adding the following code to your AppDelegate
file.
您可以通过将以下代码添加到您的AppDelegate
文件中,修改上述代码段以针对 Xcode 10 的整个应用程序工作。
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
window?.overrideUserInterfaceStyle = .light
}
#endif
However, the plist setting will fail when using Xcode version 10.x:
但是,使用 Xcode 版本 10.x 时 plist 设置将失败:
Credit to @Aron Nelson, @Raimundas Sakalauskas, @NSLeaderand rmaddyfor improving this answer with their feedback.
感谢@Aron尼尔森,@Raimundas Sakalauskas, @NSLeader和rmaddy为改善这样的回答与他们的反馈。
回答by dorbeetle
According to Apple's session on "Implementing Dark Mode on iOS" (https://developer.apple.com/videos/play/wwdc2019/214/starting at 31:13) it is possible to set overrideUserInterfaceStyle
to UIUserInterfaceStyleLight
or UIUserInterfaceStyleDark
on any view controller or view, which will the be used in the traitCollection
for any subview or view controller.
根据 Apple 关于“在 iOS 上实现暗模式”的会议(https://developer.apple.com/videos/play/wwdc2019/214/开始于 31:13),可以设置overrideUserInterfaceStyle
为UIUserInterfaceStyleLight
或UIUserInterfaceStyleDark
在任何视图控制器或视图上,这将traitCollection
用于任何子视图或视图控制器。
As already mentioned by SeanR, you can set UIUserInterfaceStyle
to Light
or Dark
in your app's plist file to change this for your whole app.
正如SeanR已经提到的,您可以设置UIUserInterfaceStyle
到Light
或Dark
应用程式的plist文件来改变这种为您的整个应用程序。
回答by Ajith R Nayak
If you are not using Xcode 11 or later (i,e iOS 13 or later SDK), your app has not automatically opted to support dark mode. So, there's no need to opt out of dark mode.
如果您使用的不是 Xcode 11 或更高版本(即 iOS 13 或更高版本 SDK),您的应用程序尚未自动选择支持暗模式。因此,无需选择退出黑暗模式。
If you are using Xcode 11 or later, the system has automatically enabled dark mode for your app. There are two approaches to disable dark mode depending on your preference. You can disable it entirely or disable it for any specific window, view, or view controller.
如果您使用的是 Xcode 11 或更高版本,系统已自动为您的应用启用暗模式。根据您的喜好,有两种方法可以禁用暗模式。您可以完全禁用它或为任何特定窗口、视图或视图控制器禁用它。
Disable Dark Mode Entirely for your App
为您的应用完全禁用暗模式
You can disable dark mode by including the UIUserInterfaceStyle
key with a value as Light
in your app's Info.plist file.
This ignores the user's preference and always applies a light appearance to your app.
您可以通过在应用程序的 Info.plist 文件中包含UIUserInterfaceStyle
带有值的键来禁用暗模式Light
。
这会忽略用户的偏好,并始终为您的应用程序应用浅色外观。
Disable dark mode for Window, View, or View Controller
禁用窗口、视图或视图控制器的暗模式
You can force your interface to always appear in a light or dark style by setting the overrideUserInterfaceStyle
property of the appropriate window, view, or view controller.
您可以通过设置overrideUserInterfaceStyle
适当的窗口、视图或视图控制器的属性来强制您的界面始终以浅色或深色样式显示。
View controllers:
视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
/* view controller's views and child view controllers
always adopt a light interface style. */
overrideUserInterfaceStyle = .light
}
Views:
意见:
// The view and all of its subviews always adopt light style.
youView.overrideUserInterfaceStyle = .light
Window:
窗户:
/* Everything in the window adopts the style,
including the root view controller and all presentation controllers that
display content in that window.*/
window.overrideUserInterfaceStyle = .light
Note: Apple strongly encourages to support dark mode in your app. So, you can only disable dark mode temporarily.
注意:Apple 强烈建议您在您的应用中支持暗模式。因此,您只能暂时禁用暗模式。
Read more here: Choosing a Specific Interface Style for Your iOS App
在此处阅读更多信息:为您的 iOS 应用选择特定的界面样式
回答by Kingsley Mitchell
********** Easiest way for Xcode 11 and above ***********
********** Xcode 11 及更高版本的最简单方法 ***********
Add this to info.plist before </dict></plist>
将此添加到 info.plist 之前 </dict></plist>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
回答by SeanR
I think I've found the solution. I initially pieced it together from UIUserInterfaceStyle - Information Property Listand UIUserInterfaceStyle - UIKit, but have now found it actually documented at Choosing a specific interface style for your iOS app.
我想我已经找到了解决方案。我最初将它从UIUserInterfaceStyle-Information Property List和UIUserInterfaceStyle-UIKit拼凑起来,但现在发现它实际上记录在为您的 iOS 应用程序选择特定界面样式中。
In your info.plist
, set UIUserInterfaceStyle
(User Interface Style) to 1(UIUserInterfaceStyle.light
).
在您的 中info.plist
,将UIUserInterfaceStyle
( User Interface Style) 设置为1( UIUserInterfaceStyle.light
)。
EDIT: As per dorbeetle's answer, a more appropriate setting for UIUserInterfaceStyle
may be Light
.
编辑:根据 dorbeetle 的回答,更合适的设置UIUserInterfaceStyle
可能是Light
.
回答by Raimundas Sakalauskas
The answer above works if you want to opt out the whole app. If you are working on the lib that has UI, and you don't have luxury of editing .plist, you can do it via code too.
如果您想退出整个应用程序,则上述答案有效。如果您正在使用具有 UI 的库,并且您无法编辑 .plist,那么您也可以通过代码来完成。
If you are compiling against iOS 13 SDK, you can simply use following code:
如果您是针对 iOS 13 SDK 进行编译,则只需使用以下代码:
Swift:
迅速:
if #available(iOS 13.0, *) {
self.overrideUserInterfaceStyle = .light
}
Obj-C:
对象-C:
if (@available(iOS 13.0, *)) {
self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
HOWEVER, if you want your code to compile against iOS 12 SDKtoo (which right now is still the latest stable SDK), you should resort to using selectors. Code with selectors:
但是,如果您也希望您的代码也针对iOS 12 SDK进行编译(它现在仍然是最新的稳定 SDK),您应该求助于使用选择器。带有选择器的代码:
Swift (XCode will show warnings for this code, but that's the only way to do it for now as property does not exist in SDK 12 therefore won't compile):
Swift(XCode 将显示此代码的警告,但这是目前唯一的方法,因为 SDK 12 中不存在该属性,因此无法编译):
if #available(iOS 13.0, *) {
if self.responds(to: Selector("overrideUserInterfaceStyle")) {
self.setValue(UIUserInterfaceStyle.light.rawValue, forKey: "overrideUserInterfaceStyle")
}
}
Obj-C:
对象-C:
if (@available(iOS 13.0, *)) {
if ([self respondsToSelector:NSSelectorFromString(@"overrideUserInterfaceStyle")]) {
[self setValue:@(UIUserInterfaceStyleLight) forKey:@"overrideUserInterfaceStyle"];
}
}
回答by kumarsiddharth123
Latest Update-
最新更新-
If you're using Xcode 10.x, then the default UIUserInterfaceStyle
is light
for iOS 13.x. When run on an iOS 13 device, it will work in Light Mode only.
如果您使用的Xcode 10.x的,则默认UIUserInterfaceStyle
是light
针对iOS 13.x. 在 iOS 13 设备上运行时,它只能在 Light Mode 下运行。
No need to explicitly add the UIUserInterfaceStyle
key in Info.plist file, adding it will give an error when you Validate your app, saying:
无需UIUserInterfaceStyle
在 Info.plist 文件中显式添加密钥,添加它会在您验证应用程序时出错,说:
Invalid Info.plist Key. The key 'UIUserInterfaceStyle' in the Payload/AppName.appInfo.plist file is not valid.
无效的 Info.plist 密钥。Payload/AppName.appInfo.plist 文件中的密钥“UIUserInterfaceStyle”无效。
Only add the UIUserInterfaceStyle
key in Info.plist file when using Xcode 11.x.
UIUserInterfaceStyle
使用 Xcode 11.x 时只在 Info.plist 文件中添加密钥。
回答by SerhiiK
If you will add UIUserInterfaceStyle
key to the plist file, possibly Apple will reject release build as mentioned here: https://stackoverflow.com/a/56546554/7524146Anyway it's annoying to explicitly tell each ViewControllerself.overrideUserInterfaceStyle = .light
. But you can use this peace of code once for your root window
object:
如果您将UIUserInterfaceStyle
密钥添加到 plist 文件,Apple 可能会拒绝此处提到的发布版本:https: //stackoverflow.com/a/56546554/7524146无论如何,明确告诉每个 ViewController很烦人self.overrideUserInterfaceStyle = .light
。但是你可以为你的根window
对象使用一次这种代码的和平:
if #available(iOS 13.0, *) {
if window.responds(to: Selector(("overrideUserInterfaceStyle"))) {
window.setValue(UIUserInterfaceStyle.light.rawValue, forKey: "overrideUserInterfaceStyle")
}
}
Just notice you can't do this inside application(application: didFinishLaunchingWithOptions:)
because for this selector will not respond true
at that early stage. But you can do it later on. It's super easy if you are using custom AppPresenter
or AppRouter
class in your app instead of starting UI in the AppDelegate automatically.
请注意,您不能在内部执行此操作,application(application: didFinishLaunchingWithOptions:)
因为此选择器true
在早期阶段不会响应。但你可以稍后再做。如果您在应用程序中使用自定义AppPresenter
或AppRouter
类而不是在 AppDelegate 中自动启动 UI,这将非常容易。
回答by Enamul Haque
回答by Mojtaba Hosseini
- For entire App (Window):
- 对于整个应用程序(窗口):
window!.overrideUserInterfaceStyle = .light
You can get the window from SceneDelegate
你可以从 SceneDelegate
- For a single ViewController:
- 对于单个 ViewController:
viewController.overrideUserInterfaceStyle = .light
You can set any viewController
, even inside the viewController itself
你可以设置 any viewController
,甚至在 viewController 里面它自己
- For a single View:
- 对于单个视图:
view.overrideUserInterfaceStyle = .light
You can set any view
, even inside the view itself
您可以设置任何view
,甚至里面的观点是自我
You may need to use if #available(iOS 13.0, *) { ,,, }
if you are supporting earlier iOS versions.
if #available(iOS 13.0, *) { ,,, }
如果您支持较早的 iOS 版本,则可能需要使用。