Reputation: 1360
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
}
Upvotes: 33
Views: 29697
Reputation: 537
You just simply need to hide the navigation controller and the statusBar
will also hidden.
try this viewDidLoad
example
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(false, animated: false)
}
Upvotes: 0
Reputation: 164
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.
If you are using a UINavigationController
as your window.rootViewController
from your AppDelegate
, then you might want to extend UINavigationController
in order to call its topViewController
's prefersStatusBarHidden
:
class YourNavigationController: UINavigationController {
override var childForStatusBarHidden: UIViewController? {
return topViewController
}
}
Upvotes: 12
Reputation: 9131
Property prefersStatusBarHidden
is being called on the root view controller of the current view controller.
This means that if your app is based on a UISplitViewController
for example, you must implement the property in a custom UISplitViewController
class.
Upvotes: 2
Reputation: 101
If you have other window
not hidden, the method not be called. Just hide other window, it will work as you wish
Upvotes: 1
Reputation: 63
For Swift 4.2 iOS 12
Assuming you have a ViewController
contained within UINavigationController
. Create your own Subclass of UINavigationController
and include in it:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Make sure info.plist
sets View Controller
based setting of status bar
Upvotes: 1
Reputation: 790
Firstly, View controller-based status bar appearance
in the .plist
file must be set to YES.
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
}
.m
file, just implement:For Objective-C:
- (BOOL)prefersStatusBarHidden {
return YES;
}
For Swift:
override func prefersStatusBarHidden() -> Bool {
return true
}
Upvotes: 40
Reputation: 323
For swift 3, first, make sure that View controller-based status bar appearance
is set to YES
in your Info
plist file
And then just add this to your view controller:
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
I hope this helps people in the future.
Upvotes: 16
Reputation: 9346
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.
Upvotes: 11
Reputation: 1360
Figured it out. in info.plist file: view controller-status bar appearance should be set to YES
Upvotes: 24