How to hide Navigation and Status Bars when tapped - with animation - ios

I'm trying to hide my Status Bar and Navigation bar when the view is tapped. Currently I found something that works from a previous question, but the problem is that there is no animation when hiding the bars. It just disappears.
Here is my current code in my View Controller:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.hidesBarsOnTap = true
}
override func prefersStatusBarHidden() -> Bool {
if self.navigationController?.navigationBarHidden == true {
return true
}
else
{
return false
}
}
When I tap again, the animation works when the two bars are coming back on to the screen.
If I don't include the overrided prefersStatusBarHidden function, I can get the navigation bar to hide with the desired sliding animation. But the status bar is still there.
Any suggestions? Does Swift 2 have a new method that can work?

Try this
var statusBarHidden = false
func tapAction() {
self.navigationController?.navigationBarHidden = true
self.statusBarHidden = true
self.setNeedsStatusBarAppearanceUpdate()
}
override func prefersStatusBarHidden() -> Bool {
return statusBarHidden
}

Have you set View controller-based status bar appearance = NO in the info.plist?

Related

Hide status bar failed

There are two viewcontrollers.
Controller A with status bar.
Controller B without status bar.
When I push A to B, there is still the status bar in B.
To hide the status bar in B. I set below in B.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.isStatusBarHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.isStatusBarHidden = false
}
I have tried to set the prefersStatusBarHidden to true, but it failed.
When I use present, there is no status bar in B.
Do you know the reason why it doesn't work? What's wrong with the codes?
Thanks in advance.
override this method in your ViewController B
override var prefersStatusBarHidden: Bool {
return true
}
override same method in your ViewController A
override var prefersStatusBarHidden: Bool {
return false
}
Include this in your plist file.
View controller-based status bar appearance" flag in Info.plist to YES

Cannot hide status bar in a specific view controller in iOS 11, Swift 4

I've got a generic UIViewController in which I would like to hide the status bar. I've got more view controllers which should display the status bar, but this specific view controller should hide the status bar.
I've implemented the following methods in the UIViewController class:
override func viewDidLoad() {
super.viewDidLoad()
// FIXME: hide status bar
var prefersStatusBarHidden: Bool {
return true
}
setNeedsStatusBarAppearanceUpdate()
}
override func viewWillAppear(_ animated: Bool) {
UIApplication.shared.isStatusBarHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
UIApplication.shared.isStatusBarHidden = false
}
In my info.plist, I've set up the following setting:
The status bar does not hide when I navigate to that view controller and is still visible.
override prefersStatusBarHidden in your view controller:
override var prefersStatusBarHidden: Bool {
return true
}
Set a value No for View Controller based status bar appearance and then show/hide your status bar for specific view controller.
Here is result:
In view controller where you want to hide the status bar,
In the viewWillAppear method, UIApplication.shared.isStatusBarHidden = true,
In the viewWillDisAppear method, UIApplication.shared.isStatusBarHidden = false
To turn off the status bar for some view controllers but not all, remove this info.plist entry if it exists OR set it to YES:
View controller-based status bar appearance = YES
Then add this line to each view controller that needs the status bar hidden
override var prefersStatusBarHidden: Bool { return true }
To turn off the status bar for the entire application, add this to info.plist:
View controller-based status bar appearance = NO
This will allow the "Hide status bar" to work as expected. Check the hide status bar located in the project's General settings under Deployment Info.
UIApplication.shared.isStatusBarHidden = true
above this Setter for 'isStatusBarHidden' was deprecated in iOS 9.0
so use below code it's working fine :)
override var prefersStatusBarHidden: Bool {
return true
}
App Delegate
swift 4.2
NotificationCenter.default.addObserver(self, selector: #selector(videoExitFullScreen), name:NSNotification.Name(rawValue: "UIWindowDidBecomeHiddenNotification") , object: nil)
#objc func videoExitFullScreen() {
UIApplication.shared.setStatusBarHidden(false, with: .none)
}
In Swift 5
override var preferredStatusBarStyle: UIStatusBarStyle {
return .default
}
Add following line in your ViewController
extension UIViewController {
func prefersStatusBarHidden() -> Bool {
return true
}
}

Hide status bar only on modally presented view controller

I have a view controller A that shows the status bar on top. From that view controller I want to present another view controller B that hides the status bar. In order to achieve that I override the property
override var prefersStatusBarHidden: Bool {
return true
}
on B. For enforcing a smooth animation whenever the status bar (dis)appears I also override the property
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
However, when I now present view controller B from A the status bar disappears abruptly while A is still visible, right before the animated modal transition begins.
I'm searching for a way to fix this "jumping status bar" behavior. Ideally, I would like to have a clean separation:
A: shows status bar
B: does not show status bar
so that, when I present B, the status bar is covered by it.
As the status bar seems to be a global view that doesn't belong to any particular view controller it's probably difficult to achieve that kind of behavior. So in case it's not possible to replicate this exact animation behavior, I'd also be happy if the status bar slides out smoothly during the view controller transition. How can I achieve that?
For animating the status bar during the transition, you could do something like this in view controller B:
var willAppear = false
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
override var prefersStatusBarHidden: Bool {
return willAppear
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
willAppear = true
UIView.animate(withDuration: 0.5) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
Then I guess you would need to do the opposite if you want the reverse effect when the modal controller is dismissed.
You can adjust the duration of the animation to whatever suits you, though I'm not sure how consistent the duration between viewWillAppear and the modal controller actually being fully presented will be.
EDIT:
"Opposite" ends up being something like this (in view controller A):
var willAppear = false
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
override var prefersStatusBarHidden: Bool {
return willAppear
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let _ = presentedViewController as? B {
willAppear = true
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if let _ = presentedViewController as? B {
willAppear = false
UIView.animate(withDuration: 0.5) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
Which I agree, is way too much code for something that I imagine many people want.

Hide Status Bar during a Custom UIViewController Transition

Background: I implemented a Custom UIViewController Transition where the first view controller (VC1) has a visible status bar
override func prefersStatusBarHidden() -> Bool {
return false
}
while the second presented view controller (VC2) has a hidden status bar:
override func prefersStatusBarHidden() -> Bool {
return true
}
Transitioning is controlled by the user since I implemented a pull to open transition with gesture controllers.
Objective: I want the status bar to be hidden during appearance transition AND disappearance transition (essentially like the Google Maps Slide Out Menu).
Problem: The status bar is correctly hidden during the entire appearance transition of ViewController VC2. But during the entire disappearance transition the status bar is visible. Any suggestions on how to properly implement this for iOS 9?
Just try to set status bar hidden on viewWillAppear and viewWillDisappear function.
You can create an instance var to hold the status bar hidden state and return this boolean from prefersStatusBarHidden(). When this value changes, call setNeedsStatusBarAppearanceUpdate().
For example:
var statusBarHidden = true {
didSet {
if oldValue != statusBarHidden {
setNeedsStatusBarAppearanceUpdate()
}
}
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
statusBarHidden = false
}

How can I show/hide the status bar on a pageViewController using tap gesture (iOS8 / Swift)

Looking through all the solutions given to similar questions, I have been trying to get the statusBar to show/hide with a tap gesture.
I have set View controller-based status bar appearance = NO in the plist.
I have tried the following code in my DataViewController (page view controller) AND in the RootViewController:
let app = UIApplication.sharedApplication()
app.setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
and it doesn't work.
This is embedded in a UITabBarController, would that make a difference?
Also, I was able to get the following to hide the statusBar from the RootViewController:
override func prefersStatusBarHidden() -> Bool {
return true
}
But the DataViewController does not even call this function, and was only able to hide it permanently this way, rather than toggle it on/off.
Any ideas?
I have tried it in code, everything works fine for me. Make sure that the View controller-based status bar appearance is Set to NO. And there is no needs to override prefersStatusBarHidden().
if you using UIPageViewController then you should use this code in the RootViewController
if you have a navigationController it will hide it too
on ViewDidLoad()
self.navigationController?.hidesBarsOnTap = true
and use this method to hide or show the status Bar based on if the navigationBar is hidden or not
override func prefersStatusBarHidden() -> Bool {
if self.navigationController?.navigationBarHidden == true {
return true
}
else
{
return false
}
}

Resources