Style status bar when navigation bar is hidden - ios

I know that with UIKit usually you just override:
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
in your UIViewController (or UINavigationController if it exists). It works perfectly fine.
However, I run into a problem when I perform this:
navigationController?.setNavigationBarHidden(true, animated: true)
Now, I can see no navigation bar, which is expected and perfectly fine. However, I see the status bar with the dark font, what is unexpected (my navigation controller overrides above property and it works properly when the navigation bar is not hidden). I want to see the status bar, but I want it in a light font. Navigation controller from this point does not listen to preferredStatusBarStyle, so I can't set it up this way.
Is there any way to display .lightContent status bar style when the navigation bar is hidden..?
The end effect is visible on the screenshot. If you zoom in, you can see dark letters & battery on dark background.
PS. Please do not post answers only about SwiftUI (here we support old iOS as well) & deprecated stuff.

Found a solution to make SDK ask for style when there is a navigation controller and the navigation bar is hidden but the status bar is shown.
In UINavigationController subclass, you need to override
override var childForStatusBarStyle: UIViewController? { return viewControllers.last }
And then inside these controllers, you can specify
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
For some reason, if the navigation bar is hidden, iOS SDK does not ask navigation controller for preferredStatusBarStyle. However, it still asks childForStatusBarStyle and we've got an issue fixed :)

Try to set your status bar style to light
After that set View controller-based status bar appearance to NO in your info.plist
UPDATE
If you want the light content only in one view you can try to override the user interface style to dark in viewDidLoad
if #available(iOS 13.0, *) {
self.overrideUserInterfaceStyle = .dark
}

Related

Swift 3 - how to hide Status Bar when using Over Full Screen

I'm developing a swift app and I can't find how to hide Status Bar when I use Over Full Screen presentation on my modal.
However, I put this line of code in my Modal View Controller :
override var prefersStatusBarHidden: Bool {
return true
}
And it is working if I create a segue which is not a modal, or if I create a segue which is a modal but not with Over Full Screen presentation.
I searched on internet how to fix it, and I found people who had the same problem but there had no solution.
Also, I can't change the color of my Status Bar when I'm using Over Full Screen option. I don't understand why? I think it's related.
Thanks for your help!
To hide the status bar when doing an over full screen modal, you need to set this in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
modalPresentationCapturesStatusBarAppearance = true
}
Then do the standard method to hide the status bar:
override var prefersStatusBarHidden: Bool {
return true
}
We can override preferredStatusBarStyle from individual view controller as you have done correctly.
Along with this, insert a new key named “View controller-based status bar appearance” and set the value to NO in your info.plist.
By disabling the “View controller-based status bar appearance”, you can set the status bar style by using the following code.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; //objective-c
Hence it should solve "I can't change the color of my Status Bar when I'm using Over Full Screen option"

Hiding status bar not working Swift 3, Xcode 8.0

Set Target/General/Deployment info to Hide status bar.
Set None for Status Bar in VCs in storyboards.
Added the following code to all VCs.
override var prefersStatusBarHidden: Bool {
return true
}
Briefly hides status bar but immediately reappears.
Only the prefersStatusBarHidden of the root-level view controller matters — here, the split view controller. The split view controller wants a status bar; it gets a status bar. That is all that matters.
You could try subclassing UISplitViewController, setting prefersStatusBarHidden in your subclass, and using that subclass in the app.

Changing the statusBar color to light in specific ViewController

I've researched the internet and found out how i'm suppose to do however when i try it nothing seem to happen. it is still dark? what am i doing wrong?
Set View controller-based status bar appearance to YES
set self.setNeedsStatusBarAppearanceUpdate() in ViewDidLoad
add below code.
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
First Set View controller-based status bar appearance to NO.
and can set like this
navigationViewController.navigationBar.barStyle = UIBarStyle. LightContent
It's possible something else is overriding your setting. For example, if you're in a navigation controller, you might find you need to subclass UINavigationController to have that set the status bar colour.
Use it in viewWillAppear
UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: false)

Cannot set iOS Status Bar Colour for View

I am developing an app for iOS8, using Swift 1.2.
However, I am having an issue with the colour of the status bar (the one with the time, battery indicator etc).
In my Info.plist file, I have UIViewControllerBasedStatusBarAppearance set to YES as well as Status bar style set to UIStatusBarStyleLightContent and then in all my view controllers in the Storyboard, I have the status bar set to "Light Content".
This works for all of my NavigationViewControllers and views embedded within NavigationViewControllers, however I have one normal TableViewController which is not embedded in a NavigationController, and when I push this view modally, the status bar changes to BLACK!???
Even when I look at the view in the Storyboard editor it shows as a white status bar (note the faint white battery indicator at the right of the below screenshot):
But when I build and run on my iPhone, the status bar shows as black...
Why is this? How can I fix this? I don't know what could be incorrect.
Please make sure to add View controller-based status bar appearance (UIViewControllerBasedStatusBarAppearance) with value NO to your Info.plist
UPDATE:
I found the solution to this was very easy, from another StackOverflow article (Swift UIApplication.setStatusBarStyle Doesn't work).
For anyone else wanting to set the status bar colour programmatically, I just inserted the following code into my ViewController for the view in question:
- Swift 4.0+
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
- Earlier Swifts (4.0-)
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
These are setups for UIStatusBar style:
Go to AppDelegate.swift in that add below code line in didFinishLaunchingWithOptions method:
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
Add below keys into .plist file.
In Storyboard select your controller UINavigationController or UIViewController. And set status bar style as Light Content
For Swift 3
in your AppDelegate.swift file in function didFinishLaunchingWithOptions
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
add this Line in your Appdelegate.swift file :
UIApplication.sharedApplication().statusBarStyle = .LightContent

iOS 8 light status bar not working

I've set the background color of the main UIView in my view controller to a bluish color.
I've also tried all combinations of the following:
Adding this to the app delegate:
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
Setting View controller status based application to NO and YES
Setting 'Status Bar Style' to Light in the project overview.
I'm seeing black status bar text when I want to see white text.
I'd like to set the style at the application level, not the VC level.
My info.plist:
for all IOS 9+
In your plist file change add/change your table with these 2 lines.
1) View controller-based status bar appearance to NO
2) Status bar style to UIStatusBarStyleLightContent
If you want to change style in running app for any reason use this
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
this saved my life numerous times! :-)
Status bar style is determined (by default) at the level of the view controller, not the application. Implement preferredStatusBarStyle in your view controller.
class ViewController : UIViewController {
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
You can determine status bar style at the application level, but to do so, you must throw a switch in your Info.plist. See the docs:
To opt out of the view controller-based status bar appearance behavior, you must add the UIViewControllerBasedStatusBarAppearance key with a value of NO to your app’s Info.plist file, but doing so is not recommended [my italics, and I don't even know whether this is even supported any longer].
Swift 4
In Info.plist add this property
View controller-based status bar appearance to NO
and after that in AppDelegate inside the didFinishLaunchingWithOptions add these code
UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = .lightContent
you can use this code
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
might be Solve this issue.

Resources