How to change iOS status bar color in child view controller

change status bar color swift
preferredstatusbarstyle not working
preferredstatusbarstyle objective-c
view controller-based status bar appearance
change status bar text color ios
property does not override any property from its superclass preferredstatusbarstyle
swift status bar background color
how to change the status bar color in storyboard

(iOS 7 Xcode 5.0.2)

I used following methods, successfully change the status bar color to white on root view controller

[self setNeedsStatusBarAppearanceUpdate]; // Update status bar style

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent; // Set status bar color to white
}

Now I'm trying to change status bar color to black when navigate to child view controller, I don't know how to do it.(status bar color is still white)

I searched, and find this method: childViewControllerForStatusBarStyle I read Apple's document,But still don't know how to/where to use it, and I'm not sure if this is the right approach

Anyone knows how to change status bar color in child view controller?

By default, it seems that UINavigationController unfortunately doesn't provide a sensible default implementation of childViewControllerForStatusBarStyle. By implementing this method, you can tell your navigationController to defer all calls to preferredStatusBarStyle to its topmost childViewController.

You could either subclass UINavigationController and implement the method there, or simply add a category:

@implementation UINavigationController (ChildStatusBarStyle)

- (UIViewController *)childViewControllerForStatusBarStyle 
{
    return self.topViewController;
}

@end

Change Status Bar Color, When you want a child viewController to control the presentation of the status bar (i.e. if the child is positioned at  However the view controller mentioned above is embedded in a navigation controller, which is also embedded in a slideout menu. Even though all embedded view controllers are added to the hierarchy using AddChildViewController() , attempting to update the status bar in a child view controller has no effect.

I just find out: When you embed the root view controller inside UINavigationController correctly, You'd never need to create a category to expand the capability of navigation controller, or subclassing UINavigationController for the same purpose.

You just need to put preferredStatusBarStyle inside every view controller, and remember to call [self setNeedsStatusBarAppearanceUpdate]; to update status bar style. Simple as it is!

check out this video from WWDC 2013: Click Here


EDIT:

The reason I made it working, is I happen to set UINavigationBar hidden. In this case, it behaves the same when not using UINavigationController at all. When you Trying to change StatusBarStyle of an UIViewController which is inside UINavigationController stack. It will fail to work in this way. It only works in individual UIViewController. The WWDC 2013 Video example is not using UINavigationController, so that why the approach is working fine.

The Mysterious Case of the Status Bar - craiggrummitt, Just call a method to update the status bar, easy. Oh wait I forgot to mention — the isStatusBarHidden and statusBarStyle properties were deprecated in iOS 9. Remember that View controller-based status bar appearance  UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true) I use this one to change status bar to light in all app. But now I need to change it in just one

James Frost answer was the only one that worked for me. Thank you! Here's a Swift 4 version of that code.

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

Note: this is a bit unwieldy as-is, I recommend adding some code to limit its scope to a single viewController. Something like this:

extension UINavigationController {
override open var childViewControllerForStatusBarStyle: UIViewController? {
    if topViewController is MyViewController {
        return topViewController
    } else {
        return nil
    }
}
}

You'll obviously need to replace MyViewController with your UIViewController subclass that implements preferredStatusBarStyle.

override var preferredStatusBarStyle: UIStatusBarStyle {
    if isBackgroundDark() {
        return .lightContent
    } else {
        return .default
    }
}

Again isBackgroundDark() is yours to implement.

Finally don't forget to call setNeedsStatusBarAppearanceUpdate() in your viewController every time isBackgroundDark() changes its value.

Change Status Bar Color / Changing the status bar style for the , Change Status Bar Color / Changing the status bar style for the entire application / Essential iOS. View controller-based status bar appearance. and set its value to. NO 1 Getting started with iOS 171 UIScrollView with StackView child. Change View controller-based status bar appearance in your info.plist to YES. Now any full screen view controller can change the status bar style by overriding preferredStatusBarStyle . I specify full screen because this will not work for (non-full screen) modal view controllers, not without setting modal Presentation Captures Status Bar Appearance to Yes that is.

In contrast to what James Frost said, and after much time spent debugging why my Browser Activities had wrong StatusBar colors (Swift):

override func childViewControllerForStatusBarStyle() -> UIViewController? {
    return visibleViewController
}

That said: In some scenarios .topViewController is right, in others like with UIActivities it's .visibleViewController.

How to change Status Bar text color in iOS, Build and Run the project to see the content of the status bar changed to light. Next, go back to the Storyboard, Select the View Controller and in  Status bar content color can be modified per view controller based, but SwiftUI uses, most usually, only one view controller, root hosting view controller. So it needs to push that root controller to change preferredStatusBarStyle property, which in base class is read-only.

Thanks to @James Frost, the solution works well. I didn't make it work at first, so I want to make a further explanation about it.

If you have a subclass of UINavigationController, it's important to add preferredStatusBarStyle in your UINavigationController subclass at the same time.

- (UIStatusBarStyle)preferredStatusBarStyle
{
  return UIStatusBarStyleLightContent;
}

And add childViewControllerForStatusBarStyle in extension of UINavigationController.

extension UINavigationController {
  override open var childViewControllerForStatusBarStyle: UIViewController? {
    return visibleViewController
  }
}

BTW, it's OK that UINavigationController subclass and extension of UINavigationController use different coding language, still work.

childViewControllerForStatusBarStyle, iOS 7.0+; Mac Catalyst 13.0+ If your container view controller derives its status bar style from one of its child view controllers, implement this method and If you return nil or do not override this method, the status bar style for self is used. I need to let a specific ViewController embedded in an UINavigationController to have light status bar text color (but other ViewControllers to behave differently). I am aware of at least 3 methods

Custom color for the status bar (iOS11+, Swift4+) - St4k, Changing the Status Bar Color for specific ViewControllers using Swift in iOS8 in iOS 9.0: Use -[UIViewController preferredStatusBarStyle] – airowe Then in the childViewController I just called the delegate method when  If you change the return value for this method, call the set Needs Status Bar Appearance Update() method. To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the child For Status Bar Hidden method. By default, this method returns false with one exception.

How to change iOS status bar color in child view controller, (iOS 7 Xcode 5.0.2) I used following methods, successfully change the status bar color to white on root view controller [self  The navigation bar is having an orange color and the status bar is having a black background color with white text color. However, when I run the same application on iOS 7, I observe the status bar looks transparent with the same orange background color as the navigation bar and the status bar text color is black.

iOS, Step 1:#. In your Info.plist add the following attribute: View controller-based status bar appearance. and set its value to. NO. as described in the image below:. If your container view controller derives the hidden state of the status bar from one of its child view controllers, implement this method to specify which child view controller you want to control the hidden/unhidden state. If you return nil or do not override this method, the status bar hidden/unhidden state for self is used.

Comments
  • Is your child view controller inside a navigation controller?
  • @JamesFrost Yes, it is.
  • I am not sure if supplying a category on a method that is declared in a subclass (UINavigationController inherits UIViewController) method is such a good idea - if I am not mistaken, this leads to undefined behaviour as a category is not a proper override. Hence I would suggest going with the subclassing instead. Still, its a good answer! (+1)
  • @JamesFrost Really good suggestion,I'm starting to figure out how to use it now. Let me try it and get back to you later. Thanks very much! (+1)
  • @JamesFrost From the documentation: The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This class is not intended for subclassing. Instead, you use instances of it as-is in situations where you want your application’s user interface to reflect the hierarchical nature of your content. I think it UINavigationControler is not sugguested for subclassing by Apple. Is there any other approach?
  • @Till Hi Till, Take a look at the info above~
  • Hmm... I've just created a new Xcode project with a single viewController. I implemented preferredStatusBarStyle to return UIStatusBarStyleLightContent, and then created a UINavigationController category containing the above method - and it worked straight away.
  • Could you specify what "embed the root view controller inside UINavigationController correctly" means? I created a simple blank project and currently have a UIViewController embedded via storyboard. The preferredStatusBarStyle is never called.
  • @chourobin You are right. and My answer above is misleading. The reason I made it working, is I happen to set UINavigationBar hidden. In this case, it behaves the same when not using UINavigationController at all.
  • @chourobin When you Trying to change StatusBarStyle of an UIViewController which is inside UINavigationController stack. It will fail to work in this way. It only works in individual UIViewController. The WWDC 2013 Video example is not using UINavigationController, so the approach is working fine. You probably want to the other way :)