Why doesn't my iOS app disable dark mode?

别等时光非礼了梦想. 提交于 2020-04-10 08:30:50

问题


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

First Attempt

Override the Interface Style for a Window, View, or View Controller

I tried to put this code sample in my 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. 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. Setting this key to Light causes the system to ignore the user's preference and always apply a light appearance to your app.

Nothing changed

Apple Documentation: Choosing a Specific Interface Style for Your iOS App

If anyone knows how I set my app only in light mode... I'll be very grateful :D


回答1:


Simply you can add a new key UIUserInterfaceStyle in your app info.plist and set its value to 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




回答2:


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

Should work. Call it in your AppDelegate's didFinishLaunchingWithOptions.




回答3:


Change the window UserInterfaceStyle for iOS 13+ version. Just set

UIApplication.shared.changeStatusBarStyle(.light)

or

UIApplication.shared.changeStatusBarStyle(.dark)

after changing window every time.

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.



来源:https://stackoverflow.com/questions/58609422/why-doesnt-my-ios-app-disable-dark-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!