I'm trying to hide the statusbar on the landingpage of my app only. I figured this is the right function and it does get executed, however the status bar still remains there
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
FBSDKLoginManager().logOut()
self.prefersStatusBarHidden()
}
override func prefersStatusBarHidden() -> Bool {
return true
}
What am I doing wrong?
Try this.
Add below entries in info.plist
View controller-based status bar appearance -> YES
Status bar is initially hidden -> YES
And In ViewControllers, In which you want to hide the StatusBar, Write below method.
override var prefersStatusBarHidden: Bool {
return true
}
Set value "No" for key "View controller-based status bar appearance" in plist file
and No need to call "self.prefersStatusBarHidden()" manually so remove it from viewDidAppear
Related
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
}
}
I'm trying to implement a button that changes the background of my app from white to black along with the colour of the status bar. Right now I have
#IBAction func buttonTapped(_ sender: Any) {
self.view.backgroundColor = UIColor.black
UIApplication.shared.statusBarStyle = .lightContent
}
This doesn't seem to work. Does anyone know of a way of changing the colour of the status bar font without reloading the entire view?
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file.
if you wan to set status bar style, at view controller level then follow these steps:
Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Set value of .plist according to status bar style setup level.
i wanna know if this can be done, i'm working on IOS 10, xCode 8 and swift 3, i tried various solutions from here but none works:
i tried to override the prefersStatusBarHidden, i tried to assign a false value but it's a get-only property and in appdelegate, i can't do this:
application.statusBarHidden = true
finally, i set in the plist the following:
Status bar is initially hidden to YES View
View controller-based status bar appearance to NO
and had no effect, i believe that all this solutions don't work because the upgrade to IOS 10.
Even after hiding the status bar for the entire app using:
application.isStatusBarHidden = true
AVPlayerViewController still showed the status bar. On going back to the presenting view controller (for which status bar was hidden earlier) status bar became visible. Tried to override prefersStatusBarHidden on both presenting and presented view controllers to no avail.
The only thing that worked was using deprecated method setStatusBarHidden in the presenting view controller's viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.setStatusBarHidden(true, with: .none)
}
You can hide the status bar in any or all of your view controllers just by adding this code:
override var prefersStatusBarHidden: Bool {
return true
}
Any view controller containing that code will hide the status bar by default.
If you want to animate the status bar in or out, just call setNeedsStatusBarAppearanceUpdate() on your view controller – that will force prefersStatusBarHidden to be read again, at which point you can return a different value. If you want, your call to setNeedsStatusBarAppearanceUpdate() can actually be inside an animation block, which causes the status bar to hide or show in a smooth way.
from: https://www.hackingwithswift.com/example-code/uikit/how-to-hide-the-status-bar
This work for me:
override var prefersStatusBarHidden: Bool {
get {
return true;
}
}
Simply subclass the AVPlayerViewController:
class PlayerViewController: AVPlayerViewController {
override var prefersStatusBarHidden: Bool {
return true
}
}
and use PlayerViewController()
This can be solved using extension of AVPlayerViewController:
Add the following line to AVPlayerViewController
extension AVPlayerViewController{
override open var prefersStatusBarHidden: Bool {
return true
}
}
Do this steps:
In info.plist file set
View controller-based status bar appearance = NO
in AppDelegate.swift file
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// use this code to hide status bar
application.isStatusBarHidden = true
return true
}
This codes enough to hide status bar in swift 3.
When I run my application, you can see the navigation bar animates in very quickly, it seems like I only catch the end. The animation also seems like it slides down from the top. I have written no code for it to do this! That's why it's weird!
Here's the weirder thing. In the info.plist if I set "View controller-based status bar appearance" to "NO" this does NOT happen. It only happens when I set it to "YES"!
I want "View controller-based status bar appearance" to be set to "YES" because I want to be able to turn it off on specific view controllers using:
override func prefersStatusBarHidden() -> Bool {
return true
}
Please help!
Pay attention to View controller-based status bar appearance in plist file, it means, that you will determine logic of status bar inside your app.
Do you assume that UIStatusBar and UINavigationBar are the same?
Definetly not.
So you function:
override func prefersStatusBarHidden() -> Bool {
return true
}
is hiding status bar (20px bar with battery, clocks and mobile cell).
If you want to change it's animation you should use:
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return .None
}
If you want to hide UINavigationBar in some view controller, you should call inside view controller:
navigationController?.setNavigationBarHidden(true, animated: false)
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
}
}