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

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


当前回答

我的应用程序不支持黑暗模式,现在使用一个浅的应用程序栏颜色。我可以通过在Info.plist中添加以下键来强制状态栏内容为深色文本和图标:

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDarkContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

在这里找到其他可能的值:https://developer.apple.com/documentation/uikit/uistatusbarstyle

颤振的用户

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

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

其他回答

如果你想退出整个应用程序,上面的答案是有效的。如果你在有UI的库上工作,并且你没有编辑.plist的奢侈,你也可以通过代码来做。

如果你正在编译iOS 13 SDK,你可以简单地使用以下代码:

迅速:

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

Obj - c:

if (@available(iOS 13.0, *)) {
    self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}

然而,如果你希望你的代码也能在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")
    }
}

Obj - c:

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

首先,这是苹果关于退出黑暗模式的词条。 这个链接的内容是为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用他们的反馈改进了这个答案。

iOS 14.3和Xcode 12.3更新

在信息。plist文件添加外观为光。

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

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

如果你将添加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,这非常简单。