我的应用程序的很大一部分由web视图组成,以提供尚未通过本机实现提供的功能。网络团队没有计划为网站实施黑暗主题。因此,我的应用程序在iOS 13上的暗模式支持看起来会有点一半一半。

是否可以选择退出暗模式支持,这样我们的应用程序总是显示光模式,以匹配网站主题?


当前回答

我想我找到解决办法了。我最初从UIUserInterfaceStyle -信息属性列表和UIUserInterfaceStyle - UIKit拼凑在一起,但现在发现它实际上记录在为你的iOS应用程序选择特定的界面风格。

在你的信息里。将UIUserInterfaceStyle (User InterfaceStyle)设置为1 (UIUserInterfaceStyle.light)。

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

其他回答

首先,这是苹果关于退出黑暗模式的词条。 这个链接的内容是为Xcode 11和iOS 13编写的:

整个应用程序通过信息。plist文件(Xcode 12)

在你的信息中使用以下键。plist文件:

UIUserInterfaceStyle

并为其赋值Light。

uiuserinterfacstyle赋值的XML:

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

UIUserInterfaceStyle的苹果文档


整个应用程序通过信息。plist在构建设置(Xcode 13)


整个应用程序窗口通过窗口属性

你可以针对应用程序的窗口变量设置overrideUserInterfaceStyle。这将应用于出现在窗口中的所有视图。这在iOS 13中是可用的,所以对于支持以前版本的应用程序,你必须包含可用性检查。

根据您的项目是如何创建的,这可能在AppDelegate或SceneDelegate文件中。

if #available(iOS 13.0, *) {
    window?.overrideUserInterfaceStyle = .light
}

单独的UIViewController或UIView

你可以针对UIViewControllers或UIView的overrideUserInterfaceStyle变量设置overrideUserInterfaceStyle。这在iOS 13中是可用的,所以对于支持以前版本的应用程序,你必须包含可用性检查。

斯威夫特

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设置时,视图及其子视图采用已定义的模式。

overrideUserInterfaceStyle的苹果文档


通过SwiftUI View的个人视图

您可以将preferredColorScheme设置为浅色或深色。提供的值将设置演示的配色方案。

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Light Only")
            .preferredColorScheme(.light)
    }
}

首选配色方案的苹果文档


感谢@Aron Nelson, @Raimundas Sakalauskas, @NSLeader和@rmaddy用他们的反馈改进了这个答案。

斯威夫特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
    }

只需简单地添加以下关键在您的信息。Plist文件:

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

Xcode 12和iOS 14更新。我已经尝试了之前的选项选择退出黑暗模式和这句话在信息。Plist文件不适合我:

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

现在它被重命名为:

<key>Appearance</key>
<string>Light</string>

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

编辑:

修复了错别字,谢谢@sarah

是的,你可以跳过添加以下代码在viewDidLoad:

if #available(iOS 13.0, *) {
        // Always adopt a light interface style.
        overrideUserInterfaceStyle = .light
    }