Swift UIApplication.setStatusBarStyle Doesn't work - ios

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!

Related

Status Bar Style issues since Xcode 11.4

Since Xcode 11.4 overriding the preferredStatusBarStyle property does not seem to work anymore in some cases.
We have an extension of UINavigationController where we override it for basically every ViewController but this property is no longer called since Xcode 11.4. Therefore the status bar is black for mostly all ViewControllers.
extension UINavigationController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
It also stopped working for some ViewControllers where we override preferredStatusBarStyle again. The property gets accessed, however, the status bar does not change its color.
View controller-based status bar appearance is set to YES in info.plist.
Issue occurs on simulator and real devices.
Does anyone have this problem too?
Ok I found a solution. Looks like I need to set the barStyle of the navigationBar to .black as described here
navigationController?.navigationBar.barStyle = .black
Not sure though why this is necessary now.
The UINavigationController extension is no longer needed then.
change the UIStatusBarStyle in appdelegates in this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}

StatusBar Hidden Issue in IOS 9

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
}

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.

how do I properly change my status bar style in swift 2/ iOS 9?

I'm attempting to change my status bar's style to .Light but the previous code I implemented in swift 1.2 seems not to work anymore.. here's the code:
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().statusBarStyle = .LightContent
}
now I have my View controller-based status bar appearance info.plist setting to YES, and reading the UIKit doc, this will negate any statusBarStyle changes and keep it at default. However when I change the setting to 'NO' and change the statusBarStyle, I get this <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable in my debugger.. So is this a bug in Xcode? because to change the status bar style you must change info.plist setting to NO, but when that happens.. error
Apple have added the capability to change the status bar style in the deployment info. Simply choose 'Light'.
Also set View controller-based status bar appearance key to NO in the Info.plist
I always did this way.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//Changing Status Bar
override func preferredStatusBarStyle() -> UIStatusBarStyle {
//LightContent
return UIStatusBarStyle.LightContent
//Default
//return UIStatusBarStyle.Default
}
}
It works in any swift 2.x version. This requires that you set View controller-based status bar appearance in your Info.plist file to YES.
Swift 3 just add View controller-based status bar appearance with value NO to info.plistand then add to ViewControllerwhere you want:
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
You can still use preferredStatusBarStyle in your view controller:
step 1: in the info.plist set ViewControllerBasedStatusBarAppearance to YES.
step 2: add this code to the ViewController you'd like to edit :
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
*** Tip: It seems to only work outside of the ViewDidLoad(), didReceiveMemoryWarning() functions.
The change in deployment info works but despite - you need to add the
'View controller-based status bar appearance' key to plist file setting it to NO.
You can also just add this in the AppDelegate. This option is better if you want to change it for every ViewController in the app and not have to make it different for every other VC.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.statusBarStyle = UIStatusBarStyle.LightContent
// instead of
// UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
// which gives warning about deprecation in iOS 9
return true
}
It looks like it's a bug in Xcode 7.0. I'm also getting the Error>: CGContextSaveGState: invalid context 0x0. error when setting View controller-based status bar appearance
For now I'm just overriding the status bar color in every view controller.
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
You can choose "light" in the deployment info, but then you also need to add the "View controller-based status bar appearance" and set it to NO.
Here try this it might help you
First goto info.plist file and add this "View controller-based status bar appearance" as a key and set the value as NO
here below shown in the image
after this come to AppDelegate.swift file and past this UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent line of code in
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool{
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
return true
}
like this
For swift 3 override the preferredStatusBarStyle variable use this:
override var preferredStatusBarStyle: UIStatusBarStyle{
return .lightContent
}
The existing answers are great, but it's a little different now with the new updates!
override var now instead of override func for anyone confused - the gist is still the same and you still need to change your 'Info.plist':
override var preferredStatusBarStyle: UIStatusBarStyle
{
//LightContent
return UIStatusBarStyle.lightContent
//Default
//return UIStatusBarStyle.default
}
If you want to change it from time to time inside your app, you can use the overrides preferredStatusBarStyle() as mentioned before.
Just make sure, that you also call setNeedsStatusBarAppearanceUpdate() after calling preferredStatusBarStyle(), to inform IOS about it.

Resources