StatusBar Hidden Issue in IOS 9 - ios

I am having one issue using status bar. I am using stroyboard id to navigate from oneviewcontroller to secondViewcontroller. I want to hide second Viewcontroller Status bar, but its not hiding and OneViewcontroller has status bar not hidden. I am using iOS 9 , Swift 2.2 and Xcode 7.3.1
override func prefersStatusBarHidden() -> Bool {
if statusBarIsVisible {
return false
} else {
return true
}
}

In your secondViewController add this method
override func prefersStatusBarHidden() -> Bool {
return true
}
This will hide status bar on secondViewController

1.Go to Info.plist file
2.Hover on one of those lines and a (+) and (-) button will show up.
3.Click the plus button to add new key Type in start with capital V and automatically the first choice will be View controller-based status bar appearance.
4.Add that as the KEY.
Set the VALUE to "NO"
5.Go to you AppDelegate.swift
Add the code, inside the method
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
application.statusBarHidden = true
return true
}

Related

Is it possible to change Status Bar color for all view controllers?

I have been searching for a while now and I only found answers that describe to change color on one view controller not for all view controllers.
Is it possible to do it?
Only two steps are needed to change the status bar style for the entire app. 🙂
Step 1
Add a new property to the project's Info.plist file and set it to false.
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Step 2
Go to your project's target and under General / Deployment Info, switch Status Bar Style from Default to Light.
Doing these steps will ensure the status bar behaves the same in the entire project.
Set the style of the status bar in AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
return true
}
And add the following code to your Info.plist:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
First in info.plist set View controller-based status bar appearance to NO
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = UIColor.blue
}
UIApplication.shared.statusBarStyle = .lightContent
return true
}
The output screenshot is below
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file.
Or programatically you can do it from app delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
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.
Swift 4
In AppDelegate.swift add this extension:
extension UINavigationController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Yes,
Step 1:
Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO
Step 2:
Open the viewcontroller file where you want to change the statusBarStyle and put the following code in viewWillAppear(),
UIApplication.shared.statusBarStyle = .lightContent
Step 3 :
Also, implement the viewWillDisappear() method for that specific viewController and put the following lines of code,
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}
All the best
Important clarification
It is very important to understand two approaches to customizing the status bar.
Is it possible to change Status Bar color for all view controllers?
Boolean answer is Yes, but, in legal way, it is so close to No that answering Yes is provocative if you need colors other that black or white.
There are two approaches when it comes to customizing Status bar appearance.
First approach – one color for whole app
In info.plist you find or create a key called
View controller-based status bar appearance
and set it to NO.
What it does? It essentially establishes a setting that says that in your application, status bar appearance is not defined individually by each view controller. This is super important to understand. This means that you have uniform setting for entire app, for all screens. This is what you needed. But. You are limited to only two settings: white and black. And there's no way you can customize it using documented API.
To set this up:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
Second approach – individual color for each view controller
This is the opposite. To make it work, go ahead to info.plist and set
View controller-based status bar appearance
to YES
This way, whenever a new view controller is open, status bar style is set individually if you insert this implementation in each UIViewController instance you need:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
You have the same as in first, set either dark or light style for statusbar.
Third approach – Hack!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let statusbar = value(forKey: "statusBar") as? UIView {
statusbar.backgroundColor = UIColor.blue
}
return true
}
Why hack? If you need status bar color other than black or white. Here you use undocumented API. You get statusBar object using KVC and manually set its color. This is dirty, not legal way, but so far it's the only way to set up custom color for statusbar. It may well lead your app to being rejected. But maybe you're lucky. In order to set it once and for all, you will need to set to NO the aforementioned flag so that status bar did not initialize its style with each view controller.

Hide Status Bar In certain View controllers *Specific*

I have a very specific question concerning hiding status bar.
I incorporated the code below to easily get a status bar on every view controller, but I have a cameraViewcontroller where I don't want a status bar. I'm trying to hide the status bar.
//change the statusbar color for all view controllers
application.statusBarStyle = .lightContent
I tried using
override var prefersStatusBarHidden: Bool {
return true
}
and
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.isStatusBarHidden = true
return true
}
but none of this seems to work. I'm new to programming and stack overflow so please help me out. I tried search for the answer but can't seem to find the answer to this particular dilemma. Your help will be much appreciated! Thank you.
its works for me perfectly.
Objective-C
-(BOOL)prefersStatusBarHidden{
return YES;
}
Swift
override var prefersStatusBarHidden: Bool {
return true
}
you should also use info.plist
"View controller-based status bar appearance" set to NO
"Status bar is initially hidden" set to YES
Can you apply below code in the CameraVC :
override var prefersStatusBarHidden: Bool {
return true
}

How to hide the status bar in an AVPlayerViewController?

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.

Hide statusbar during splash screen

I'm trying to hide the statusbar during splash screen, which works fine when i add "Status bar is initially hidden" to plist and set value to YES, however this remove the statusBar from the enitre application, even though i've added "View controller-based status bar appearance" to plist and set value to NO and added following to appdelegate:
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
How can i remove the statusBar initially without removing it in the rest of the application?
This is updated for Swift 3 of Xcode 8.3.3
In your Info.plist add the following key:
Then in your AppDelegate file add the following in didFinishLaunchingWithOptions section:
func application(_application:UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.isStatusBarHidden = false
return true
}
That should sort out your problem.
You can also configure the launch colour in your project Build Settings if this is a problem for you:
In Swift 4
In Info.plist add:
Status bar is initially hidden YES
In your Project Settings -> General-> Deployment Info, check "Hide status bar" field.
Next in your view controller override - prefersStatusBarHidden method, like this:
override func prefersStatusBarHidden() -> Bool {
return false
}
Just add the highlighted line into your Info.plist file and it'll work for Swift 4:
In Swift 3
First Hide Status bar from Project->Target as following
then unhide status bar in didFinishLaunchingWithOptions
func application(_application:UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.isStatusBarHidden = false
return true
}
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation)
To also hide on iPad, also set the UIStatusBarHidden~ipad key in the Info.plist to YES.

Swift UIApplication.setStatusBarStyle Doesn't work

Here's my implementation in a subclass of UIViewController:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
}
I tried putting it in AppDelegate initialisation code (using the passed in UIApplication instance), however the status bar is still black..
Is this a bug with iOS 8 or am I doing it wrong?
Note: I may be breaking someone's law's here by discussing ios 8.. Its a general problem that, when compiled doesn't work for ios 7 either.
Update: Still doesn't work, even with value in Info.plist and code in - didFinishLaunchingWithOptions
You really should implement preferredStatusBarStyle on your view controller(s):
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
Go to Info.plist file
Hover on one of those lines and a (+) and (-)
button will show up.
Click the plus button to add new key Type in
start with capital V and automatically the first choice will be View
controller-based status bar appearance.
Add that as the KEY.
Set the VALUE to "NO"
Go to you AppDelegate.swift
Add the following code, inside the method
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
application.setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
return true
}
DONE! Run your app and your status bar is now WHITE!

Resources