eagle.dan.1349
eagle.dan.1349

Reputation: 631

Getting current iOS 7 statusbar style

Good time of day everyone! iOS 7 welcomed us with many not-so-funny and not-so-documented 'features', especially in terms of appearance cusomization. My issue is the following:

I set 'View controller-based status bar appearance' to YES in my application plist, how do I now get current statusbar style programmatic? Old code like

[UIApplication sharedApplication].statusBarStyle

always returns UIStatusBarStyleDefault disregard the style from viewcontroller.

To prevent future misunderstanding: I have NO intention to make statusBarStyle property working, I'm looking for new way with 'View controller-based status bar appearance' turned on. PLEASE, obstinate from 'advices' to turn it off.

Upvotes: 1

Views: 1579

Answers (4)

Sebastian Hojas
Sebastian Hojas

Reputation: 4210

I was using an overlay UIWindow without knowing which view controller is currently responsible for the status bar style, and therefore couldn't use other solutions. What worked for me is

UIApplication.shared.keyWindow?.rootViewController?
     .childViewControllerForStatusBarStyle.preferredStatusBarStyle

What the documentation says:

Called when the system needs the view controller to use for determining status bar style.

Upvotes: 1

Zaid Pathan
Zaid Pathan

Reputation: 16820

This worked for me, I wanted to use presentingViewController's StatusBar style,

- (UIStatusBarStyle) preferredStatusBarStyle
{
    return [self.presentingViewController preferredStatusBarStyle];
}

Upvotes: 0

eagle.dan.1349
eagle.dan.1349

Reputation: 631

Found my answer... If View controller-based status bar appearance is turned on you should get style like this:

[[[[UIApplication sharedApplication] keyWindow] rootViewController] preferredStatusBarStyle]

And you should take into account such things as navigation/tab-bar controllers. If you have your own you can do the following things.

- (UIStatusBarStyle) preferredStatusBarStyle
{
    return [[ self selectedViewController] preferredStatusBarStyle];
}

for your subclass of tab-bar controller and

- (UIStatusBarStyle) preferredStatusBarStyle
{
    return [[self visibleViewController] preferredStatusBarStyle];
}

for tab-bar controller.

Upvotes: 2

Raon
Raon

Reputation: 1286

Try this..

 UIStatusBarStyle style=[[UIApplication sharedApplication] statusBarStyle];

Upvotes: 2

Related Questions