How to hide status bar of a single view controller in iOS 9?

后端 未结 15 1276
挽巷
挽巷 2020-12-13 06:15

In a ViewController, which I presented modally, I did this:

override func prefersStatusBarHidden() -> Bool {
    return true
}

This used

15条回答
  •  孤城傲影
    2020-12-13 06:53

    For those still struggling, the below works for iOS9.

    You update the rootViewController prefersStatusBarHidden function by calling it from your individual child/grandchild viewControllers. This works where you add childViewControllers directly to your rootViewController.

    You don't need to set anything in the info.plist, but setting setting 'statusBarIsInitiallyHidden' works independently of the below.

    First, in your rootViewController, add the following:

    -(void)viewDidLoad {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateStatusBarAppearance:) name:@"kStatusBarAppearance" object:nil]; 
    }
    -(void)updateStatusBarAppearance:(NSNotification *)n {
        statusBarIsHidden = [n.object boolValue];
        [self setNeedsStatusBarAppearanceUpdate];
    }
    -(UIStatusBarStyle)preferredStatusBarStyle {
        return UIStatusBarStyleLightContent; //optional
    }
    -(BOOL)prefersStatusBarHidden{
        return statusBarIsHidden;
    }
    

    Then, in the single view controller where you want to hide the status bar, call this:

    -(void)viewDidLoad {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kStatusBarAppearance" object:[NSNumber numberWithBool:true]];
    }
    -(void)popSelf {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kStatusBarAppearance" object:[NSNumber numberWithBool:false]];
    }
    

提交回复
热议问题