在iOS 13上可以选择退出暗模式吗?

Sea*_*anR 143 ios13 ios-darkmode

我的应用程序很大一部分由Web视图组成,以提供本机实现尚不可用的功能。Web团队没有计划为网站实施深色主题。因此,在iOS 13上支持深色模式的情况下,我的应用看起来会一半/一半。

是否可以选择不支持暗模式,以便我们的应用程序始终显示亮模式以匹配网站主题?

Cod*_*der 392

首先,这是与退出暗模式有关的Apple条目此链接的内容适用于Xcode 11和iOS 13

本部分适用于Xcode 11的用法


如果您希望退出整个申请

方法1

在info.plist文件中使用以下密钥

UIUserInterfaceStyle
Run Code Online (Sandbox Code Playgroud)

并为其分配值Light

XMLUIUserInterfaceStyle分配:

<key>UIUserInterfaceStyle</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)

方法#2

您可以overrideUserInterfaceStyle针对应用程序的window变量进行设置。

根据项目的创建方式,该AppDelegate文件可能位于文件中,也可能位于中SceneDelegate

if #available(iOS 13.0, *) {
    window?.overrideUserInterfaceStyle = .light
}
Run Code Online (Sandbox Code Playgroud)


如果您希望逐个退出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
    }
}
Run Code Online (Sandbox Code Playgroud)

Apple文档overrideUserInterfaceStyle

上面的代码在Xcode 11中的样子:

在此处输入图片说明

本部分适用于Xcode 10.x的用法


如果您使用Xcode 11进行提交,则可以放心忽略此行下的所有内容。

由于相关的API在iOS 12中不存在,因此在尝试使用上面提供的值时会出现错误:

对于设置overrideUserInterfaceStyleUIViewController

在此处输入图片说明

如果您希望逐个退出UIViewController

这可以通过测试编译器版本和iOS版本在Xcode 10中进行处理:

#if compiler(>=5.1)
if #available(iOS 13.0, *) {
    // Always adopt a light interface style.
    overrideUserInterfaceStyle = .light
}
#endif
Run Code Online (Sandbox Code Playgroud)

如果您希望退出整个申请

通过将以下代码添加到AppDelegate文件中,您可以修改上述代码段以使其适用于Xcode 10的整个应用程序。

#if compiler(>=5.1)
if #available(iOS 13.0, *) {
    // Always adopt a light interface style.
    window?.overrideUserInterfaceStyle = .light
}
#endif
Run Code Online (Sandbox Code Playgroud)

但是,使用Xcode版本10.x时,plist设置将失败:

在此处输入图片说明

感谢@Aron尼尔森@Raimundas Sakalauskas@NSLeaderrmaddy为改善这样的回答与他们的反馈。

  • 无需在每个视图控制器的“ viewDidLoad”中设置“ overrideUserInterfaceStyle”,而是可以在应用程序的主窗口中对其进行一次设置。如果您希望整个应用程序以一种方式运行,那么会容易得多。 (6认同)
  • Info.plist 中的键已更改为“Appearance”。`&lt;key&gt; 外观&lt;/key&gt; &lt;string&gt;灯光&lt;/string&gt;` (6认同)
  • 立即更新/上传应用程序时,UIUserInterfaceStyle指示灯被阻止。它被标记为无效的plist条目。(无效的plist键) (2认同)
  • 无法针对iOS SDK 12(当前最新的稳定SDK)进行编译。请参阅/sf/answers/4026533101/,以获得适用于iOS 12 SDK的解决方案。 (2认同)
  • 使用 `#if compiler(&gt;=5.1)` 代替 `responds(to:)` 和 `setValue` (2认同)

dor*_*tle 146

据苹果公司的“实施暗模式iOS上的”会话(https://developer.apple.com/videos/play/wwdc2019/214/开始于31:13),可以集overrideUserInterfaceStyleUIUserInterfaceStyleLightUIUserInterfaceStyleDark任何视图控制器或视图上,它将traitCollection用于任何子视图或视图控制器。

正如SeanR已经提到的,您可以设置UIUserInterfaceStyleLightDark在您的应用程序的plist文件来改变这种为您的整个应用程序。

  • 如果您设置UIUserInterfaceStyle键,则您的应用将在应用商店中被拒绝 (14认同)
  • 拒绝最有可能发生,因为iOS 13 SDK尚未超出Beta版。我认为这应该在Xcode 11 GM可用后立即生效。 (8认同)
  • 苹果拒绝使用ITMS-90190错误代码https://forums.developer.apple.com/thread/121028 (2认同)
  • @dorbeetle 这不是真的,我用这个密钥成功上传了我的应用程序,就像 1 个月前使用 Xcode 10 一样。最近发生了拒绝。这似乎是苹果的某种新战略。 (2认同)

Aji*_*yak 55

如果您未使用Xcode 11或更高版本(即iOS 13或更高版本的SDK),则您的应用尚未自动选择支持暗模式。因此,无需选择退出黑暗模式。

如果您使用的是Xcode 11或更高版本,则系统已自动为您的应用启用暗模式。有两种方法可以根据您的喜好禁用暗模式。您可以完全禁用它,也可以对任何特定的窗口,视图或视图控制器禁用它。

完全为您的应用禁用暗模式

您可以通过在UIUserInterfaceStyle键中包含与Light应用程序的Info.plist文件中相同的值来禁用暗模式。
UIUserInterfaceStyle为浅色
这会忽略用户的偏好,并始终为您的应用程序添加浅色外观。

对窗口,视图或视图控制器禁用暗模式

通过设置overrideUserInterfaceStyle适当的窗口,视图或视图控制器的属性,可以强制界面始终以浅色或深色显示。

查看控制器:

override func viewDidLoad() {
    super.viewDidLoad()
    /* view controller’s views and child view controllers 
     always adopt a light interface style. */
    overrideUserInterfaceStyle = .light
}
Run Code Online (Sandbox Code Playgroud)

观看次数:

// The view and all of its subviews always adopt light style.
youView.overrideUserInterfaceStyle = .light
Run Code Online (Sandbox Code Playgroud)

窗口:

/* 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
Run Code Online (Sandbox Code Playgroud)

注意:Apple强烈建议您在应用中支持暗模式。因此,您只能暂时禁用黑暗模式。

在此处了解更多信息:为iOS应用选择特定的界面样式

  • 在 XCode 12.4 中,它显示为“外观”而不是用户界面样式。 (4认同)

waz*_*ski 31

Xcode 12 和 iOS 14 更新。我已经尝试了以前的选项来选择退出暗模式,而 info.plist 文件中的这句话对我不起作用:

<key>UIUserInterfaceStyle</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)

现在它更名为:

<key>Appearance</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)

此设置将阻止完整应用程序中的所有黑暗模式。

编辑:

修正了错字,感谢@sarah


Sea*_*anR 26

我想我已经找到了解决方案。我最初将其从UIUserInterfaceStyle-信息属性列表UIUserInterfaceStyle-UIKit拼凑而成,但是现在发现它确实记录在为iOS应用选择特定界面样式中

在中info.plist,将UIUserInterfaceStyle用户界面样式)设置为1UIUserInterfaceStyle.light)。

编辑:根据dorbeetle的回答,UIUserInterfaceStyle可能更合适的设置是Light

  • 在plist中拥有此密钥将导致App Store拒绝。 (2认同)

Kin*_*ell 22

********** Xcode 11及更高版本的最简单方法***********

将此添加到info.plist之前 </dict></plist>

<key>UIUserInterfaceStyle</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)


Rai*_*kas 18

如果您想退出整个应用程序,则上述答案有效。如果您使用的是具有UI的lib,并且您对.plist的编辑并不奢侈,那么也可以通过代码来实现。

如果您要针对iOS 13 SDK进行编译,则只需使用以下代码即可:

迅速:

if #available(iOS 13.0, *) {
    self.overrideUserInterfaceStyle = .light
}
Run Code Online (Sandbox Code Playgroud)

对象C:

if (@available(iOS 13.0, *)) {
    self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您也希望代码也可以针对iOS 12 SDK进行编译(目前仍是最新的稳定SDK),则应使用选择器。带选择器的代码:

Swift(XCode将显示此代码的警告,但这是目前唯一的方法,因为SDK 12中不存在该属性,因此无法编译):

if #available(iOS 13.0, *) {
    if self.responds(to: Selector("overrideUserInterfaceStyle")) {
        self.setValue(UIUserInterfaceStyle.light.rawValue, forKey: "overrideUserInterfaceStyle")
    }
}
Run Code Online (Sandbox Code Playgroud)

对象C:

if (@available(iOS 13.0, *)) {
    if ([self respondsToSelector:NSSelectorFromString(@"overrideUserInterfaceStyle")]) {
        [self setValue:@(UIUserInterfaceStyleLight) forKey:@"overrideUserInterfaceStyle"];
    }
}
Run Code Online (Sandbox Code Playgroud)


Ena*_*que 15

您可以在 Xcode 11 的整个应用程序中关闭暗模式

  1. 去信息.plist
  2. 添加波纹管

    <key>UIUserInterfaceStyle</key>
    <string>Light</string>
    
    Run Code Online (Sandbox Code Playgroud)

Info.plist 将如下所示...

在此处输入图片说明


kum*_*123 12

最新更新-

如果您使用的是Xcode 10.x,则默认UIUserInterfaceStyle值为lightiOS13.x。在iOS 13设备上运行时,它将仅在灯光模式下工作。

无需UIUserInterfaceStyle在Info.plist文件中显式添加密钥,添加密钥将在验证应用程序时出现错误,提示:

无效的Info.plist密钥。Payload / AppName.appInfo.plist文件中的键“ UIUserInterfaceStyle”无效。

UIUserInterfaceStyle在使用Xcode 11.x时将密钥添加到Info.plist文件中。

  • @NiranjanMolkeri这与较新的iPhone无关。这是关于iOS 13上的黑暗模式的。在以前的iOS 13 Beta版本中,如果未明确处理,则UI会出现黑暗模式问题。但是在最新版本中,此问题已修复。如果您使用的是XCode 10,则iOS13的默认UIUserInterfaceStyle是light。如果您使用的是Xode11,则需要进行处理。 (3认同)

小智 9

iOS 14.3 和 Xcode 12.3 更新

在 info.plist 文件中添加Appearance as Light

<key>Appearance</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)


Ser*_*iiK 8

如果您将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")
    }
}
Run Code Online (Sandbox Code Playgroud)

只是请注意,您不能在内部执行此操作,application(application: didFinishLaunchingWithOptions:)因为对于该选择器true,它在早期不会响应。但是您以后可以做。如果您在应用程序中使用自定义AppPresenterAppRouter类,而不是自动在AppDelegate中启动UI,则超级简单。


Moj*_*ini 7

-对于整个应用程序(窗口):

window!.overrideUserInterfaceStyle = .light
Run Code Online (Sandbox Code Playgroud)

您可以从获取窗口 SceneDelegate

-对于单个ViewController:

viewController.overrideUserInterfaceStyle = .light
Run Code Online (Sandbox Code Playgroud)

您可以设置任何viewController,甚至连它的viewController内部自我

-对于单个视图:

view.overrideUserInterfaceStyle = .light
Run Code Online (Sandbox Code Playgroud)

您可以设置任何view,甚至里面的观点是自我

if #available(iOS 13.0, *) { ,,, }如果支持较早的iOS版本,则可能需要使用。


Cla*_*dio 6

除了其他响应,据我所知,您仅需要在针对iOS 13 SDK(使用XCode 11)进行编译时为Dark模式做好准备。

系统假定与iOS 13或更高版本的SDK链接的应用程序支持亮外观和暗外观。在iOS中,您可以通过为窗口,视图或视图控制器分配特定的界面样式来指定所需的特定外观。您也可以使用Info.plist键完全禁用对黑暗模式的支持。

链接


Ras*_*tif 6

斯威夫特 5

从暗模式切换到亮模式的两种方法:

1- info.plist

    <key>UIUserInterfaceStyle</key>
    <string>Light</string>

Run Code Online (Sandbox Code Playgroud)

2- 以编程方式或运行时

  @IBAction private func switchToDark(_ sender: UIButton){
        UIApplication.shared.windows.forEach { window in
            //here you can switch between the dark and light
            window.overrideUserInterfaceStyle = .dark
        }
    }
Run Code Online (Sandbox Code Playgroud)


Ton*_*ado 5

我的应用程序目前不支持暗模式并使用浅色应用程序栏颜色。通过将以下键添加到我的中,我能够将状态栏内容强制为深色文本和图标Info.plist

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDarkContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
Run Code Online (Sandbox Code Playgroud)

在此处查找其他可能的值:https : //developer.apple.com/documentation/uikit/ustatusbarstyle

颤振用户

不要忘记在 Flutter 应用栏上设置应用栏亮度属性,如下所示:

AppBar(
    backgroundColor: Colors.grey[100],
    brightness: Brightness.light, // <---------
    title: const Text('Hi there'),
),
Run Code Online (Sandbox Code Playgroud)


小智 5

在 Xcode 12 中,您可以将添加更改为“外观”。这会起作用的!