本文介绍了是否可以在 iOS 13 上选择退出暗模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

是否可以选择退出深色模式支持,以便我们的应用始终显示与网站主题相匹配的浅色模式?

解决方案

首先,这里是

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

您可以针对应用程序的 window 变量设置 overrideUserInterfaceStyle.这将适用于出现在窗口中的所有视图.这在 iOS 13 中可用,因此对于支持以前版本的应用,您必须包括可用性检查.

根据项目的创建方式,这可能位于 AppDelegateSceneDelegate 文件中.

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

单独的 UIViewController 或 UIView

您可以针对UIViewControllers 或UIViewoverrideUserInterfaceStyle 变量设置overrideUserInterfaceStyle.这在 iOS 13 中可用,因此对于支持以前版本的应用,您必须包括可用性检查.

迅捷

override func viewDidLoad() {super.viewDidLoad()//overrideUserInterfaceStyle 适用于 iOS 13如果#available(iOS 13.0,*){//始终采用轻量级的界面风格.overrideUserInterfaceStyle = .light}}

致那些在 Objective-C 中的可怜人

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

当针对 UIViewController 设置时,视图控制器及其子级采用定义的模式.

当针对 UIView 设置时,视图及其子视图采用定义的模式.

关于 overrideUserInterfaceStyle 的 Apple 文档

通过 SwiftUI 视图的个人视图

您可以将preferredColorScheme 设置为lightdark.提供的值将为演示文稿设置配色方案.

导入 SwiftUI结构内容视图:查看{var主体:一些视图{文本(仅浅色").preferredColorScheme(.light)}}

preferredColorScheme 的 Apple 文档

感谢 @Aron Nelson@Raimundas Sakalauskas@NSLeader@rmaddy 改进此答案与他们的反馈.

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.

Is it possible to opt out of Dark Mode support such that our app always shows light mode to match the website theme?

解决方案

First, here is Apple's entry related to opting out of dark mode.The content at this link is written for Xcode 11 & iOS 13:

Entire app via info.plist file (Xcode 12)

Use the following key in your info.plist file:

UIUserInterfaceStyle

And assign it a value of Light.

The XML for the UIUserInterfaceStyle assignment:

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

Apple documentation for UIUserInterfaceStyle


Entire app via info.plist in build settings (Xcode 13)


Entire app window via window property

You can set overrideUserInterfaceStyle against the app's window variable. This will apply to all views that appear within the window. This became available with iOS 13, so for apps that support previous versions, you must include an availability check.

Depending on how your project was created, this may be in the AppDelegate or SceneDelegate file.

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


Individual UIViewController or UIView

You can set overrideUserInterfaceStyle against the UIViewControllers or UIView's overrideUserInterfaceStyle variable. This became available with iOS 13, so for apps that support previous versions, you must include an availability check.

override func viewDidLoad() {
    super.viewDidLoad()
    // overrideUserInterfaceStyle is available with iOS 13
    if #available(iOS 13.0, *) {
        // Always adopt a light interface style.
        overrideUserInterfaceStyle = .light
    }
}
if (@available(iOS 13.0, *)) {
        self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}

When set against the UIViewController, the view controller and its children adopt the defined mode.

When set against the UIView, the view and its children adopt the defined mode.

Apple documentation for overrideUserInterfaceStyle


Individual views via SwiftUI View

You can set preferredColorScheme to be either light or dark. The provided value will set the color scheme for the presentation.

import SwiftUI

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

Apple documentation for preferredColorScheme


Credit to @Aron Nelson, @Raimundas Sakalauskas, @NSLeader and @rmaddy for improving this answer with their feedback.

这篇关于是否可以在 iOS 13 上选择退出暗模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:05