简体   繁体   English

为什么我的 iOS 应用程序不禁用深色模式?

[英]Why doesn't my iOS app disable dark mode?

So... I've tried to set my app to disable iOS 13 dark mode by forcing light mode according apple documentation, in the emulator all attempts work fine, but when I try on the real device, nothing happens, it's like I've never changed my code所以...我尝试将我的应用程序设置为根据苹果文档通过强制亮模式来禁用 iOS 13 暗模式,在模拟器中所有尝试都工作正常,但是当我在真实设备上尝试时,没有任何反应,就像我'我从未更改过我的代码

First Attempt第一次尝试

Override the Interface Style for a Window, View, or View Controller覆盖 Window、视图或视图 Controller 的界面样式

I tried to put this code sample in my viewDidLoad()我试图将此代码示例放入我的 viewDidLoad()

Nothing Changed没有改变

if #available(iOS 13.0, *) {
   overrideUserInterfaceStyle = .light
} else {
  // Fallback on earlier versions
}

Second Attempt第二次尝试

Opt Out of Dark Mode Entirely完全退出黑暗模式

The system automatically opts in any app linked against the iOS 13.0 or later SDK to both light and dark appearances.系统会自动选择与 iOS 13.0 或更高版本 SDK 链接的任何应用程序,以显示浅色和深色外观。 If you need extra time to work on your app's Dark Mode support, you can temporarily opt out by including the UIUserInterfaceStyle key (with a value of Light) in your app's Info.plist file.如果您需要额外的时间来处理应用程序的深色模式支持,您可以通过在应用程序的 Info.plist 文件中包含 UIUserInterfaceStyle 键(值为 Light)来暂时选择退出。 Setting this key to Light causes the system to ignore the user's preference and always apply a light appearance to your app.将此键设置为 Light 会导致系统忽略用户的偏好并始终为您的应用程序应用浅色外观。

Nothing changed没有改变

Apple Documentation: Choosing a Specific Interface Style for Your iOS App Apple 文档:为您的 iOS 应用选择特定的界面风格

If anyone knows how I set my app only in light mode... I'll be very grateful:D如果有人知道我如何将我的应用程序设置为仅在轻模式下......我将非常感激:D

Simply you can add a new key UIUserInterfaceStyle in your app info.plist (Notes: Xcode 12 and above has renamed to Appearance ) and set its value to Light or Dark .只需在应用程序info.plist中添加一个新键UIUserInterfaceStyle (注意:Xcode 12 及更高版本已重命名为Appearance )并将其值设置为Light or Dark this will override the app default style to the value you provide.这会将应用默认样式覆盖为您提供的值。

so you don't need to bother about having it anywhere else所以你不必费心在其他任何地方拥有它

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

Should work.应该管用。 Call it in your AppDelegate 's didFinishLaunchingWithOptions .AppDelegatedidFinishLaunchingWithOptions中调用它。

Change the window UserInterfaceStyle for iOS 13+ version.更改 iOS 13+ 版本的 window UserInterfaceStyle。 Just set刚设置

UIApplication.shared.changeStatusBarStyle(.light)

or或者

UIApplication.shared.changeStatusBarStyle(.dark)

after changing window every time.每次更改 window 后。

extension UIApplication {

        enum StatusColor {

            case dark, light
        }

        func changeStatusBarStyle(_ mode: StatusColor = .light) {

            if #available(iOS 13.0, *) {

                guard let appDelegate = delegate as? AppDelegate else { return }

                var interfaceStyle: UIUserInterfaceStyle

                switch mode {
                case .dark:
                    interfaceStyle = .dark
                default:
                    interfaceStyle = .light
                }

                appDelegate.window?.overrideUserInterfaceStyle = interfaceStyle
            }
        }
    }

If any confusion please let me know.如果有任何困惑,请告诉我。

Add inside Info.plistInfo.plist里面添加

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

For SwiftUI you might find the following solution helpful:对于 SwiftUI,您可能会发现以下解决方案很有用:

extension UIApplication {
    func changeInterfaceStyle(_ mode: UIUserInterfaceStyle = .light) {
        if #available(iOS 13.0, *) {
            var window: UIWindow? {
                guard let scene = connectedScenes.first,
                      let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
                      let window = windowSceneDelegate.window else {
                    return nil
                }
                return window
            }
            window?.overrideUserInterfaceStyle = mode
        }
    }
}

Usage:用法:

    Button("Dark") {
        UIApplication.shared.changeInterfaceStyle(.dark)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM