11

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
Fogaiht
  • 113
  • 1
  • 1
  • 6
  • 2
    Everything you've posted in the question is correct. You have even stated that it works in the simulator. The problem on a real device may be that the app isn't actually being updated. Delete your app from the device then reinstall through Xcode. Does it work now just as it does in the simulator? – rmaddy Oct 29 '19 at 15:59
  • Possible duplicarte of https://stackoverflow.com/questions/56537855/is-it-possible-to-opt-out-of-dark-mode-on-ios-13/64693329#64693329 – tripleee Nov 05 '20 at 08:37

5 Answers5

11

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. this will override the app default style to the value you provide.

so you don't need to bother about having it anywhere else

Antonio Adrian Chavez
  • 1,016
  • 1
  • 7
  • 12
Reed
  • 944
  • 7
  • 15
  • when I run my app in emulator, this solution work's fine, but in real device, nothing changed – Fogaiht Oct 29 '19 at 16:30
  • try uninstalling the app from device first and check if you are using the same info.plist file and also make sure you are cleaning the project – Reed Oct 29 '19 at 16:31
  • This answer doesn't actually solve the issue. The OP was already using the Info.plist key. At least redo the answer with the actual solution which was needing to delete and reinstall the app. – rmaddy Oct 29 '19 at 17:35
  • 1
    Make sure value is `Light`, not `light` (it's case sensitive) – Alexander Vasenin Nov 10 '19 at 17:44
  • 1
    this is driving me crazy. I have checked the case of "Light", cleaned the project, deleted it from both the simulator and the real device, but nothing is preventing the app from going into dark mode with dark mode set. info.plist clearly recognizes the "Appearance" property – James Joshua Street Apr 12 '21 at 02:48
  • Note that the actual key in the info.plist file is `UIUserInterfaceStyle` (Open As -> Source Code) but displays as `Appearance` (Open As -> Property List) – Thorsten Stark Mar 07 '23 at 11:28
5
if #available(iOS 13, *) {
    window.overrideUserInterfaceStyle = .light
}

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

ChrisTheGreat
  • 512
  • 4
  • 21
  • 3
    But under iOS 13, your app's window won't be in the app delegate, it will be in the scene delegate unless you have completely opted out of scenes. – rmaddy Oct 29 '19 at 15:58
2

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.

dip
  • 3,548
  • 3
  • 24
  • 36
Amrit Tiwari
  • 922
  • 7
  • 21
2

Add inside Info.plist

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

For SwiftUI you might find the following solution helpful:

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)
    }
Paul B
  • 3,989
  • 33
  • 46