7

I have multiple UITabBar in my application and some ViewController has White color statusbar and some ViewController has black color statusbar.

My info.plist

View controller-based status bar appearance to YES

My Viewcontroller has below code.

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .default //or return . lightContent
}

but preferredStatusBarStyle never getting called.

i have also written below line in my controller viewDidLoad but still above wasn't getting called.

self.setNeedsStatusBarAppearanceUpdate()

also i have changeed controller-based status bar appearance to YES && NO for multiple time to check but nothing helps to me.

I have also tried below solutions and other stackoverflow answers but nothing helps me.

preferredStatusBarStyle not respecting on iOS 13

preferredStatusBarStyle var not working in iOS12?

EDIT

I have tried below code which returns me the topViewController and it will call the preferredStatusBarStyle of that ViewController

extension UINavigationController {
    override open var childForStatusBarStyle: UIViewController? {
        return topViewController
    }
}

so once the topViewController found it will call preferredStatusBarStyle of that particular ViewController.

but the issue is that it wasn't getting called inside UITabBarController -> UINavigationController -> UIViewController.

Requirment

I have 2 different TabBarController.

1st TabBarController statusBarStyle is .lightContent.

2nd TabBarController statusBarStyle is .lightContent and .default in different controller.

When i change to the 2nd TabBarController it will call preferredStatusBarStyle of 2nd TabBarController and all ViewController statusBarStyle goes .default but some of my controller statusBarStyle wants to be of .ligthContent

How can i achieve this?

any help will be appreciated.

Thanks

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
  • I still use UIApplication.shared.statusBarStyle though it was deprecated, but it still works correctly, call it in viewWillAppear – Alexandr Kolesnik Jan 03 '20 at 11:42
  • @AlexandrKolesnik, okay let me try it. – Kuldeep Jan 03 '20 at 11:44
  • @AlexandrKolesnik, My flow like this. `TabBarController`--> `NavigationController`--> `ViewController`. – Kuldeep Jan 03 '20 at 11:50
  • @AlexandrKolesnik, i put above line in `ViewController` `viewWillAppear` then proper statusBar style display for 1 second and then it goes to .light automatically. – Kuldeep Jan 03 '20 at 11:51
  • 1
    remove from plist `controller-based status bar appearance` call `UIApplication.shared.statusBarStyle = .default setNeedsStatusBarAppearanceUpdate()` in viewWillAppear – Alexandr Kolesnik Jan 03 '20 at 11:53
  • @AlexandrKolesnik, If i remove `controller-based status bar appearance` then do i need to write `UIApplication.shared.statusBarStyle = .default`/ `.lightContent` in all `ViewController`? – Kuldeep Jan 03 '20 at 11:57
  • in my case I created two controllers for light and dark style and inherite controllers from them – Alexandr Kolesnik Jan 03 '20 at 12:08
  • @AlexandrKolesnik, i do the same for `UINavigationController`. 1 is for light and 2nd is for default but sometimg style goes worng – Kuldeep Jan 03 '20 at 12:11
  • @Kuldeep if your status bar shows correct for 1 sec then goes back to .light in 1 sec, it looks like you have some code somewhere in ```viewDidAppear``` for any of your view controllers/navigationControllers/tabBarControllers. Check if any of these implement anything in their ```viewDidAppear``` method. – Starsky Jan 13 '20 at 16:15
  • try something like this. Target->general->StatusBarStyle->default. It may work – Priya Sri Jan 14 '20 at 10:00
  • check with disabled dark mode – SPatel Jan 17 '20 at 06:03

4 Answers4

3

This has nothing to do with iOS 13. You just have the rules wrong.

In a navigation controller situation, the color of the status bar is not determined by the view controller’s preferredStatusBarStyle.

It is determined, amazingly, by the navigation bar’s barStyle. To get light status bar text, say (in your view controller):

self.navigationController?.navigationBar.barStyle = .black
H.Gadou
  • 41
  • 5
2

i got the solution.

Put below code to find the topViewController.

extension UINavigationController {
    override open var childForStatusBarStyle: UIViewController? {
        return topViewController
    }
}

so once it finds topViewController, below code getting called in your current ViewController and you can set statusBarStyle as per requirement.

override var preferredStatusBarStyle: UIStatusBarStyle { }

In my case i have 2 TabBar.

1st TabBar controllers are of .lightContent and 2nd TabBar controllers are of .default so create 2 UITabBarController. 1st is for .lightContent and 2nd is for .default and put preferredStatusBarStyle inside it.

so when you are at UITabBarController sub controller, your UITabBarController preferredStatusBarStyle getting called and sub controller statusBarStyle set as per your set style.

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
  • where should i put the extension? in the view controller. oc create a new file as extension ? Because still not working on me – lauwis Oct 19 '22 at 11:59
1

please refer to this

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621453-modalpresentationcapturesstatusb

override var modalPresentationCapturesStatusBarAppearance: Bool {
        set {}
        get{

            return true
        }
    }
Muhammad Afzal
  • 201
  • 1
  • 9
  • this will give an error like : Cannot override mutable property with read-only property 'modalPresentationCapturesStatusBarAppearance' – Kuldeep Jan 03 '20 at 12:18
  • i have updated i don't know how it missed that you need to call set and get both. – Muhammad Afzal Jan 03 '20 at 12:46
  • will you please write sample code like how can i change statusbar style through this? – Kuldeep Jan 03 '20 at 13:37
  • did you write above method and it gets called? where preferredStatusBarStyle was n't working. if you check you will see the case where preferredsta ytsubarstyle will n't called is presentation case. If it does n't work can send me code or screenshot i can help you with that. – Muhammad Afzal Jan 04 '20 at 06:14
-2

the following may be of assistance to you;

For developer guidance, see the UIStatusBarStyle constant in UIApplicenter code hereation and the preferredStatusBarStyle property in UIViewController.

Anthony
  • 23
  • 1
  • 7