How to change UIStatusBarStyle in iOS 7 in modal views with navigation bar?

后端 未结 9 2371
孤独总比滥情好
孤独总比滥情好 2020-12-31 11:32

The iOS 7 Transition Guide give a good hint how to change the UIStatusBarStyle dynamically in a UIViewController using

- (UIStatus         


        
相关标签:
9条回答
  • 2020-12-31 11:47

    We should notice that non-fullscreen modalVC CAN use modalPresentationCapturesStatusBarAppearance to control the statusBar style.

    Anyone who wanna know more about Status Bar control should not ignore the UIViewController Managing the Status Bar.

    Update at 2015-11-06:

    And make sure you have set UIViewControllerBasedStatusBarAppearance described in iOS Keys

    Update at 2018.04.09:

    I noticed that viewController in a navController may not get call prefersStatusBarHidden with iOS 10.0 - 10.2. Custom your navigationController to ensure that

    @implementation YourCustomNavController
    //for iOS 10.0 - iOS 10.2
    - (BOOL)prefersStatusBarHidden {
        UIViewController *childVC = [self childViewControllerForStatusBarHidden];
        if (childVC) {
            return [childVC prefersStatusBarHidden];
        }
        return [super prefersStatusBarHidden];
    }
    @end
    

    And anyone who want to go deeper inside can dig into UIKit +[UIViewController _currentStatusBarStyleViewController] using Hopper or IDA Pro. It may helps you solve these kinds of bugs.

    0 讨论(0)
  • 2020-12-31 11:50

    It seems like the app goes off the statusBarStyle of the topmost viewController. So if you add another viewController on top of your current one, it now gets its cues from the new viewController.

    0 讨论(0)
  • 2020-12-31 11:54

    This works for me:

    1. Set View controller-based status bar appearance to NO
    2. Set Status bar style to UIStatusBarStyleLightContent (just copy that value)
    3. In appDelegate use [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    Hope it helps (ref: ios7 status bar changing back to black on modal views?)

    0 讨论(0)
  • 2020-12-31 11:57

    I had a side menu/reveal controller (SWRevealController) which turns out to always be the root controller for status bar queries. Overriding childViewControllerForStatusBarStyle let me re-route the query to the front most controller.

    /**
     This view is always considered the topmost for status bar color queries.
     Pass the query along to what we're showing in front.
     */
    - (UIViewController *)childViewControllerForStatusBarStyle
    {
        UIViewController *front = self.frontViewController;
        if ([front isKindOfClass:[UINavigationController class]])
            return ((UINavigationController*)front).topViewController;
        else
            return front;
    }
    
    0 讨论(0)
  • 2020-12-31 12:04

    To change the status bar of the UINavigationController embedding your ViewController without subclassing UINavigationController, use this:

    navigationController?.navigationBar.barStyle = .Black // to make the status bar text white
    

    .Black will make the text white (status bar and the view's title), while .Default has a black title and status bar.

    0 讨论(0)
  • 2020-12-31 12:05

    The key to making this work is that only the fullscreen view controller get's to dictate the style of the status bar.

    If you are using a navigation controller and want to control the status bar on a per view controller basis, you'll want to subclass UINavigationController and implement preferredStatusBarStyle such that it returns the topViewController's preference.

    Make sure you change the class reference in your storyboard scene fromUINavigationController to your subclass (e.g. MyNavigationController in the example below).

    (The following works for me. If your app is TabBar based, you'll want to do something similar by subclassing the UITabBarController but I haven't tried that out).

    @interface MyNavigationController : UINavigationController
    
    @end
    
    @implementation MyNavigationController
    
    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        return self.topViewController.preferredStatusBarStyle;
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题