Preferredstatusbarstyle Isn't Called

preferredStatusBarStyle isn't called

Possible root cause

I had the same problem, and figured out it was happening because I wasn't setting the root view controller in my application window.

The UIViewController in which I had implemented the preferredStatusBarStyle was used in a UITabBarController, which controlled the appearance of the views on the screen.

When I set the root view controller to point to this UITabBarController, the status bar changes started to work correctly, as expected (and the preferredStatusBarStyle method was getting called).

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
... // other view controller loading/setup code

self.window.rootViewController = rootTabBarController;
[self.window makeKeyAndVisible];
return YES;
}

Alternative method (Deprecated in iOS 9)

Alternatively, you can call one of the following methods, as appropriate, in each of your view controllers, depending on its background color, instead of having to use setNeedsStatusBarAppearanceUpdate:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

or

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

Note that you'll also need to set UIViewControllerBasedStatusBarAppearance to NO in the plist file if you use this method.

Can not override the preferredStatusBarStyle

I am going to answer the question here, maybe it can help someone else:

After hours checking my codes, I found out that I have rootViewController which I used as a authentication then after the authentication user pass to TabbarViewController... which is not rootViewController, I added the:

preferredStatusBarStyle

To my rootViewController and it works.

View controller-based status bar appearance should be
YES

preferredStatusBarStyle not getting called in iOS 13 and other

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.

preferredStatusBarStyle is not working

Here is Apple Guidelines/Instruction about status bar change.

If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file. And in your appdelegate > didFinishLaunchingWithOptions add following ine (programatically you can do it from app delegate).

Objective C

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];

Swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}

if you want to set status bar style, at view controller level then follow these steps:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller.

Objective C

- (void)viewDidLoad
{
[super viewDidLoad];
[self setNeedsStatusBarAppearanceUpdate];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}

Swift

override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

Set value of .plist according to status bar style setup level.

Sample Image


You can set background color for status bar during application launch or during viewDidLoad of your view controller.

extension UIApplication {

var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}


or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}

}




Here is result:

Sample Image


Swift: preferredStatusBarStyle() not called using SSASideMenu

If you need to set the status bar anytime one of your subclasses of ViewController appears, you can do so in the viewDidAppear method:

func viewDidAppear(_ animated: Bool)

viewDidAppear

NOTE: You could also probably set it in func viewDidLayoutSubviews() - that fires anytime the subviews need to be laid out, I'm just not sure if the status bar is technically a subview, you could test it out - viewDidAppear is most likely an acceptable choice.

preferredStatusBarStyle var not working in iOS12?

This has nothing to do with iOS 12. 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

Hard to believe, but true. I got this info directly from Apple, years ago.

You can also perform this setting in the storyboard.

Example! Navigation bar's bar style is .default:

Sample Image

Navigation bar's bar style is .black:

Sample Image

NOTE for iOS 13 This still works in iOS 13 as long as you don't use large titles or UIBarAppearance. But basically you are supposed to stop doing this and let the status bar color be automatic with respect to the user's choice of light or dark mode.

UIStatusBarStyle PreferredStatusBarStyle does not work on iOS 7

OK, here's the trick. You do have to add the key "View controller-based status bar" and set the value to No.

This is counter to what it appears the meaning of this key is, but even if you set the value to No, you can still change the appearance of the status bar, and whether it shows or not in any view controller. So it acts like "Yes" but set it to "No"!

Now I can get the status bar white or dark.



Related Topics



Leave a reply



Submit