Swift 3 UIStatusBarStyle not working for separate ViewControllers within UINavigationController - ios

Something in Swift 3 changed.
I used to be able to change the status bar color from default to light on every ViewController even though it was wrapped within a UINavigationController.
Right now I have a UINavigationController that manages all of my UIViewControllers and when I'm doing this on each ViewController it doesn't work:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
On the other hand, when I'm inserting this code into my subclass of UINavigationCotroller it works, but I can't change it dynamically at all, meaning all of my ViewControllers now have .lightContent and I cannot change it.
Is there a solution to this horrifying change?
Thanks :)

You can change it dynamically to all views in your app by adding UIApplication.shared.statusBarStyle = .lightContent to your AppDelegate.swift file inside your didFinishLaunchingWithOptions function. You also will need to go to you info.plist file and add the Boolean View controller-based status bar appearance and set it to NO.

Related

Style status bar when navigation bar is hidden

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
}

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

How to use modalPresentationCapturesStatusBarAppearance = NO with a custom UIPresentationController?

I have a custom UIPresentationController and overrides frameOfPresentedViewInContainerView for a custom viewController presentation. Everything works fine, except for the status bar.
I do not want the status bar to change appearance at all – it should just stay however it looked before. Now the Apple Documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instp/UIViewController/modalPresentationCapturesStatusBarAppearance says if modalPresentationStyle is not UIModalPresentationFullScreen OR modalPresentationCapturesStatusBarAppearance is NO, i should be fine and the status bar should not change.
With this code:
- (BOOL)prefersStatusBarHidden {
NSLog(
#"prefersStatusBarHidden was called %d %ld",
self.modalPresentationCapturesStatusBarAppearance,
(long)self.modalPresentationStyle
);
return YES;
}
I can see that prefersStatusBarHidden is called, even if modalPresentationCapturesStatusBarAppearance is NO (displays as 0) and modalPresentationStyle is UIModalPresentationCustom (displays as 4).
Obviously, that's the reason the status bar changes when presenting the viewController.
But why?
My thought on this is that iOS thinks that the viewController is presented in fullscreen even though it is not.
I discovered UIPresentationController's property shouldPresentInFullscreen – it returns YES by default. Returning NO doesn't help at all, so i don't understand what that property even does... It has literally no effect. The same applies to the presentationStyle property – I don't see any effect when changing it. I would have expected the presentationStyle property to be "redirected" to the viewControllers modalPresentationStyle property, but that stays at UIModalPresentationCustom, which it has to be to initiate the custom presentation in the first place.
So, my questions are: Does anybody know how to just keep the status bar as it is with a custom UIPresentationController – and can somebody explain the shouldPresentInFullscreen and presentationStyle properties?
Thank you! :)
Try implementing childViewControllerForStatusBarStyle: and return nil for it in the class invoking your UIPresentationController, usually a UINavigationController.
This is what I do in Swift when I don't want a child VC to interfere with my wisely chosen Status Bar Style:
override func childViewControllerForStatusBarStyle() -> UIViewController? {
return nil // ignore childs and let this Navigation Controller handle the StatusBar style
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent // or .Default depending on your Style
}
This requires iOS8 and newer and is only usable if you are setting the key UIViewControllerBasedStatusBarAppearance in your Info.plist to YES.
Bonus: If this does not help in the caller, use it in the shown Ccontroller. I checked my projects, in one of them it's also in the NavigationController shown as PopOver and working fine as of today.

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