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

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


当前回答

import UIKit

extension UIViewController {

    override open func awakeFromNib() {

        super.awakeFromNib()

        if #available(iOS 13.0, *) {

            overrideUserInterfaceStyle = .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用他们的反馈改进了这个答案。

对于整个应用程序:(在信息。plist文件):

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


窗口(通常是整个应用程序):

window!.overrideUserInterfaceStyle = .light

你可以从SceneDelegate中获取窗口


UIViewController:

viewController.overrideUserInterfaceStyle = .light

你可以设置任何viewController,甚至在viewController内部


UIView:

view.overrideUserInterfaceStyle = .light

你可以设置任何视图,甚至在视图内部

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


SwiftUI View):

.preferredColorScheme(.light) <- This Modifier

or

.environment(\.colorScheme, .light) <- This Modifier

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

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

如果你将添加UIUserInterfaceStyle键到plist文件中,苹果可能会拒绝发布构建,如这里所述:https://stackoverflow.com/a/56546554/7524146 无论如何,显式地告诉每个ViewController self是很烦人的。overrideuserinterfacstyle = .light。但是你可以在根窗口对象上使用这段代码:

if #available(iOS 13.0, *) {
    if window.responds(to: Selector(("overrideUserInterfaceStyle"))) {
        window.setValue(UIUserInterfaceStyle.light.rawValue, forKey: "overrideUserInterfaceStyle")
    }
}

请注意,你不能在应用程序(application: didFinishLaunchingWithOptions:)中这样做,因为这个选择器在早期阶段不会响应true。但你可以以后再做。如果你在你的应用中使用自定义AppPresenter或AppRouter类,而不是在AppDelegate中自动启动UI,这非常简单。

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