I'd like to remove the status bar at the top of the screen.
This does not work:
func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
application.statusBarHidden = true
return true
}
I've also tried:
func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var controller = UIViewController()
application.statusBarHidden = true
controller.setNeedsStatusBarAppearanceUpdate()
var view = UIView(frame: CGRectMake(0, 0, 320, 568))
view.backgroundColor = UIColor.redColor()
controller.view = view
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "Hello World"
controller.view.addSubview(label)
self.window!.rootViewController = controller
self.window!.makeKeyAndVisible()
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.isStatusBarHidden = true
return true
}
Just to add, when overriding prefersStatusBarHidden
method or variable, the View controller-based status bar appearance
in Info.plist must be YES, otherwise the override will have no effect
You can use this code in your ViewController
Class
scope
open override var prefersStatusBarHidden: Bool { return true }
I actually figured this out myself. I'll add my solution as another option.
extension UIViewController {
func prefersStatusBarHidden() -> Bool {
return true
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true);
navigationController?.navigationBar.hidden = true // for navigation bar hide
UIApplication.sharedApplication().statusBarHidden=true; // for status bar hide
}
Add the code, inside the method
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
application.statusBarHidden = true
return true
}
DONE! Run your app and no more status bar!
For Swift 4+ try the following code (tested on Swift 4.0, 4.1 - IOS 10, 11) :
override var prefersStatusBarHidden: Bool { return true }
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// call this func to force preferredStatusBarStyle to be read again.
setNeedsStatusBarAppearanceUpdate()
}
Updated for iOS 13 and Swift 5
If none of the above answers work for you. Check your plist to see if you have this:
"View controller-based status bar appearance"
If so, be sure to set it to YES!!!!!
Then the following code will work.
override var prefersStatusBarHidden: Bool {
return true
}
If you are presenting the view controller modally, try
viewController.hidesBottomBarWhenPushed = true
viewController.modalPresentationCapturesStatusBarAppearance = true
Okay, so this become a problem for me since iOS 9 doesn't support any above the method people have mentioned here such as UIApplication.sharedApplication().statusBarHidden = true
or
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)
and
override func prefersStatusBarHidden() -> Bool {
return true
}
works but does not provide programable solution where I can change on a condition.
(statusBarHidden = true
and statusBarHidden = false
as we have done before).
Solution to this madness:
By adding to prefersStatusBarHidden()
like below you can programmatically control the hide and show of status bar without adding UIViewControllerBasedStatusBarAppearance
setting to your info.plist:
var showStatusBar = true
override func prefersStatusBarHidden() -> Bool {
if showStatusBar {
return false
}
return true
}
private func showStatusBar(enabled: Bool) {
showStatusBar = enabled
prefersStatusBarHidden()
}
then use it like this throughout your code:
//Hide Status Bar
showStatusBar(false)
OR
//Show Status Bar
showStatusBar(true)
In my case, I need to update the status bar hidden based on some conditions.
Because of this, I create a base controlller BaseViewController
which contains new property hideStatusBar
.
Other view controllers are sub-class of this base controller. Finally when I want to update the status bar behavior, I only need to change this hideStatusBar
value.
class BaseViewController: UIViewController {
var hideStatusBar: Bool = false {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
override var prefersStatusBarHidden: Bool {
return hideStatusBar
}
}
final class ViewController: BaseViewController, UIScrollViewDelegate {
let scrollView = UIScrollView()
...
func scrollViewDidScroll(_ scrollView: UIScrollView) {
UIView.animate(withDuration: 0.3) {
if scrollView.contentOffset.y > 100 {
self.hideStatusBar = true
} else {
self.hideStatusBar = false
}
}
}
}
Here is a demo, I'm using UIView.animate(...)
to make the transition smoother.
In your project General->Deployment Info->Status bar style select check mark of Hide status bar Note:- it hides status bar throughout application
Update for iOS 10 / Swift 3.0
No longer a function, now a property...
override var prefersStatusBarHidden: Bool {
return true
}
In my case, I was looking for the status bar to hide/show on demand; instead of just when the view loads or disappears.
swift 3.x
//show status bar initially
var showStatusBar = true
//set the parameters
override var prefersStatusBarHidden: Bool {
if showStatusBar == true {
//does not prefer status bar hidden
print("does not prefer status bar hidden")
return false
} else {
//does prefer status bar hidden
print("does prefer status bar hidden")
return true
}
}
//ex: hide status bar and call parameter function again whenever you want
showStatusBar = false
setNeedsStatusBarAppearanceUpdate()
A solution that works for me; if you want to hide the status bar on a specific view controller while loading:
import UIKit
class ViewController: UIViewController {
private var hideStatusBar: Bool = false
override var prefersStatusBarHidden: Bool {
return hideStatusBar
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return UIStatusBarAnimation.slide
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundcolor = .white
hideStatusBar = true
UIView.animate(withDuration: 0.3) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
Attention: if you set the key "View controller-based status bar appearance" to "NO" in your info.plist the code above doesn't work. You should set the key to "YES" or remove it from info.plist
Swift 5: In the main view controller, or main navigation controller if you have,
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override var prefersStatusBarHidden: Bool {
return false
}
And "View controller-based status bar appearance" in plist must be YES, otherwise the above code will not be called.
If you want to hide status bar when launching app, "Status bar is initially hidden" in plist must be YES. This can prevent launch image from being distorted when extra blue bar showing on screen top.
Swift 4
//MARK:- Show Status Bar
UIApplication.shared.isStatusBarHidden = false
//MARK:- Hide Status Bar
UIApplication.shared.isStatusBarHidden = true
So the issue here actually has nothing to do with Swift but just how status bar appearance is handled as of iOS 7.
By Default, view controllers individually control the appearance of the status bar when they are on the screen. If you want to use this method of controlling the status bar, you can override the following methods for whatever view controllers you'd like to modify the appearance for:
prefersStatusBarHidden
,
preferredStatusBarStyle
,
preferredStatusBarAnimation
,
In your case, you would just implement prefersStatusBarHidden
and return true
.
The other way would be to control the status bar appearance at the application level. This seems to be what you're actually trying to do (by setting application.statusBarHidden
).
In order to make this work, you need to open up your app's Info.plist
file and add the key UIViewControllerBasedStatusBarAppearance
, and give it a value of NO
.
in Swift 3.x:
override func viewWillAppear(_ animated: Bool) {
UIApplication.shared.isStatusBarHidden = true
}
Swift 3
In Info.plist
set View controller-based status bar appearance
to NO
And call UIApplication.shared.isStatusBarHidden = true
In your Project->General->Deployment info
Statusbar Style:--
just marked Hide status Bar(iOS 10)
If you want to hide and bring back the status bar on button tap, while at the time of presenting and dismissing slide-in menu, popups etc, then you can use this method:-
To hide the status bar:-
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
To bring back the status bar:-
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal
I'm using Xcode 8.1 (8B62) with a deployment target set to 10.1 and I haven't had much luck with the override options mentioned above. However checking the "Hide status bar" option in Deployment Info did the trick for me.
I hope this helps.
in Swift 4.2 it is a property now.
override var prefersStatusBarHidden: Bool {
return true
}
Source: Stackoverflow.com