Kuldeep
Kuldeep

Reputation: 4552

preferredStatusBarStyle not getting called in iOS 13 and other

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

Upvotes: 8

Views: 3603

Answers (4)

H.Gadou
H.Gadou

Reputation: 41

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

Upvotes: 3

Kuldeep
Kuldeep

Reputation: 4552

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.

Upvotes: 2

Anthony
Anthony

Reputation: 11

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.

Upvotes: -2

Muhammad Afzal
Muhammad Afzal

Reputation: 201

please refer to this

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

override var modalPresentationCapturesStatusBarAppearance: Bool {
        set {}
        get{

            return true
        }
    }

Upvotes: 1

Related Questions