Search code examples
swiftstatusbarios13

Stop changing status bar in iOS 13


I have already a light and dark theme in my app. I also supported iOS 13 dark mode, the problem is, even when the light or dark mode of the app is selected, changing iOS theme leads to change status bar color, so when the ios dark mode is on, the status bar is invisible in the light theme in my app. I tried to prevent it by this block of codes, but it won't work in my case:

override var preferredStatusBarStyle: UIStatusBarStyle {
    switch Appearance.theme {
    case .dark:
        return .lightContent
    case .light:
        if #available(iOS 13.0, *) {
            return .darkContent
        } else {
            return .default
        }
    case .apple:
        return .default
    }
}

I added it in he split view controller, I also add it in each controller but nothing changes. Does anyone have any idea how to fix it? I aslo have same problem for UITabbar and UISegmentedControl

Somehow I should say that when the light and dark mode in my app is selected, changing iOS theme should effect in the app element (status bar, tabbar, UISegmentedControl)

Many Thanks


Solution

  • You can use overrideUserInterfaceStyle to force controllers to adopt a specific style in iOS13.

    To solve your problem, your code should be like that:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        switch Appearance.theme {
        case .dark:
            return .lightContent
        case .light:
            if #available(iOS 13.0, *) {
                return .darkContent
            } else {
                return .default
            }
        case .apple:
            return .default
        }
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if #available(iOS 13.0, *) {
             switch Appearance.theme {
             case .dark:
                 overrideUserInterfaceStyle = .dark
             case .light:
                 overrideUserInterfaceStyle = .light
             case .apple:
                 overrideUserInterfaceStyle = .unspecified
             }
         }
    }