Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 652
Robert Avatar asked Sep 20 '19 21:09

Robert


People also ask

How do I turn off status bar on Iphone?

Go to Your info. plist file. Add a key called “View controller-based status bar appearance” and set its value to NO.

Can we change status bar color in iOS?

You can change the status bar colour just with a single line of code. Just updated the markdown for iOS 13 and below.


1 Answers

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
         }
     }
}
like image 161
Rouzbeh Avatar answered Sep 27 '22 19:09

Rouzbeh