prefersStatusBarHidden not called

2020-05-25 01:38发布

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
}

标签: ios statusbar
9条回答
在下西门庆
2楼-- · 2020-05-25 01:54

Maybe not a solution to the OP problem, but what could also be the cause for prefersStatusBarHidden not being called is if you have used a second window in your app delegate, eg for displaying a splash screen, and you did not hide it after the splash was shown - then that window gets the events that lead to calling these functions.

查看更多
小情绪 Triste *
3楼-- · 2020-05-25 02:04

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
}
查看更多
Emotional °昔
4楼-- · 2020-05-25 02:04

If you have other window not hidden, the method not be called. Just hide other window, it will work as you wish

查看更多
地球回转人心会变
5楼-- · 2020-05-25 02:05

When we nested the UINavigationController, our AppDelegate. Window. RootViewController Usually we create navigationController when first will call in the navigationController childViewControllerForStatusBarHidden function, because the default returns nil, then is called navigationController prefersStatusBarHidden function itself, So the status bar that we set in viewController through the prefersStatusBarHidden function will not be called, so it won't work. So we're going to create our own one that inherits from the NavigationController,in this subclass ChildViewControllerForStatusBarHidden function.

查看更多
【Aperson】
6楼-- · 2020-05-25 02:08

For swift 3, first, make sure that View controller-based status bar appearance is set to YES in your Info plist file

screenshot

And then just add this to your view controller:

override var prefersStatusBarHidden: Bool {
    get {
        return true
    }
}

I hope this helps people in the future.

查看更多
做个烂人
7楼-- · 2020-05-25 02:09

Figured it out. in info.plist file: view controller-status bar appearance should be set to YES

查看更多
登录 后发表回答