How to Hide The Status Bar Programmatically in iOS 8

How to hide the status bar programmatically in iOS 8

You should add this key/value pair to your project's Info.plist.

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

Sample Image

After that,calling

UIApplication.sharedApplication().statusBarHidden = true

or

UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .Fade) // with animation option.

This post gives more details > How to hide iOS status bar

Hide Status Bar In iOS 8 app

You need to override this method on each view controller unless you have that plist entry.

Objective-C

-(BOOL)prefersStatusBarHidden{
return YES;
}

Swift 2

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

Swift 3+

override var prefersStatusBarHidden: Bool {
return true
}

And don't forget to set (if you present a view controller by calling the presentViewController:animated:completion: method):

Objective-C

vcToBeShownWithoutStatusbar.modalPresentationCapturesStatusBarAppearance = YES;

Swift

vcToBeShownWithoutStatusbar.modalPresentationCapturesStatusBarAppearance = true

Documentation: https://developer.apple.com/reference/uikit/uiviewcontroller/1621453-modalpresentationcapturesstatusb

If you change status bar from some container view controller (eg. UINavigationController or UIViewController with child view controllers) and you would like to change view controller responsible for status bar you should use childViewControllerForStatusBarHidden: property. Eg:

Set first view controller instance always responsible for status bar management

Objective-C

- (UIViewController *)childViewControllerForStatusBarHidden {
return childViewControllers.first; // or viewControllers.first
}

Swift 2

override var childViewControllerForStatusBarHidden() -> UIViewController? {
return childViewControllers.first // or viewControllers.first
}

Swift 3+

override var childViewControllerForStatusBarHidden: UIViewController? {
return childViewControllers.first // or viewControllers.first
}

Set container view controller responsible for status bar management

Objective-C

- (UIViewController *)childViewControllerForStatusBarHidden {
return nil;
}

Swift 2

override func childViewControllerForStatusBarHidden() -> UIViewController? {
return nil
}

Swift 3+

override var childViewControllerForStatusBarHidden: UIViewController? {
return nil
}

Documentation:
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621451-childviewcontrollerforstatusbarh

How do I hide the status bar in a Swift iOS app?

You really should implement prefersStatusBarHidden on your view controller(s):

Swift 3 and later

override var prefersStatusBarHidden: Bool {
return true
}

How to hide status bar gradually in swift

There is no way to move the status bar; it is either visible or hidden. However, you can get around this by replacing the status when the UITableView is about to be scrolled with a UIView containing a screenshot that includes the status bar.

I would start with the UIScrollViewDelegate. Since the UITableViewDelegate is a subclass of the UIScrollViewDelegate, you can, in your view controller, set your UITableView's delegate to self. With this, you can now listen to scrolling movements.

When the UITableView is at the top and the user is scrolling down, I would recommend to take a screenshot of the status bar, add it to a UIView, and move that said view up at the same speed as the UITableView is moving (see the UIScrollViewDelegate documentation for help). Likewise, when scrolling to the top, I would have the view move down to the position of the status bar, then remove the view, and set the status bar visible.

With this method, you have the appearance of the status moving up and down with the table view.



Related Topics



Leave a reply



Submit