Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 480
Strong84 Avatar asked Jan 18 '14 17:01

Strong84


People also ask

Can we change status bar color in IOS?

Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content. The style of the status bar can be changed to a status bar with white content.

How can I change status bar background color?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.

How do I change the color of my status bar flutter on my Iphone?

Step 1: Locate the MaterialApp widget. Step 2: Inside the MaterialApp, add the theme parameter with ThemeData class assigned. Step 3: Inside the ThemeData add the appBarTheme parameter and then assign the AppBarTheme class. Step 4: Inside the AppBarTheme , specify the systemOverlayStyle parameter and set the color.


Video Answer


3 Answers

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
like image 154
James Frost Avatar answered Oct 19 '22 02:10

James Frost


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.

like image 32
Cortis Clark Avatar answered Oct 19 '22 01:10

Cortis Clark


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.

like image 1
Strong84 Avatar answered Oct 19 '22 02:10

Strong84