prefersStatusBarHidden not called

Md1079 picture Md1079 · Nov 5, 2015 · Viewed 25.1k times · Source

I have a UITabViewController -> UINavigationController -> UIViewController and want to hide and unhide the statusBar. when I call setNeedsStatusBarAppearanceUpdate() the method prefersStatusBarHidden is not called.

func fadeOutStatusBar (notification: NSNotification) {
    statusBarHidden = true
    self.setNeedsStatusBarAppearanceUpdate()
}

func fadeInStatusBar (notification: NSNotification) {
    statusBarHidden = false
    self.setNeedsStatusBarAppearanceUpdate()
}

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

Answer

Nghia Luong picture Nghia Luong · Nov 5, 2015

Firstly, View controller-based status bar appearance in the .plist file must be set to YES.

  • If you want status bar to be hidden in the whole app:

For Objective-C:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [application setStatusBarHidden:YES];

    return YES;
}

For Swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
    application.statusBarHidden = true

    return true
}
  • If you want status bar is disappeared in Specify View Controller, in .m file, just implement:

For Objective-C:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

For Swift:

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