Status bar style not changing for individual views - Swift 2/iOS9 - ios

I would like to have the default status bar style set to some view controllers but not others. For the other views I would like to set it to lightcontent.
After taking the advice from another post I have tried setting View controller-based status bar appearance to YES in info.plist and add then adding the following code to viewController.swift (in an attempt to only change the status bar style of the viewcontroller in question):
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
However, this does not work despite suggestions that it does here: how do I properly change my status bar style in swift 2/ iOS 9?.
What is the best solution?

Managed to solve this issue by firstly deleting View controller-based status bar appearance in info.plist then adding this to my navigation view controller:
extension UINavigationController {
public override func childViewControllerForStatusBarHidden() -> UIViewController? {
return self.topViewController
}
public override func childViewControllerForStatusBarStyle() -> UIViewController? {
return self.topViewController
}
Then I added this to the viewController that was connected to my navigationController:
override public func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
override func prefersStatusBarHidden() -> Bool {
return false
}
Edit: You can choose to ignore any warnings that xcode gives you relating to this last step - the first function can be changed from public to internal and it should still work.

Related

prefersStatusBarHidden issue in iOS 13

Hi everyone I'm trying to hide my statusBar in a View Controller but it doesn't seem to work .. I used the function:
override var prefersStatusBarHidden: Bool {
         return true
    }
I also set the View controller-based status bar appearance in the plist file to YES
My status bar doesn't want to hide ... where am I doing wrong?
It looks like you're trying to specifically hide the status bar in a single ViewController.
In order to do that, you need have the following in that ViewController
self.modalPresentationCapturesStatusBarAppearance = true
override var prefersStatusBarHidden: Bool {
return true
}
I also added View controller-based status bar appearance in my .plist and set it to YES.
Tested on the latest iOS 13.
If the target view controller is embedded in another container view controller such as UINavigationController you need to subclass that container view controller and override its childForStatusBarHidden to return the target view controller.
you kan look this,you should override childForStatusBarHidden and childForStatusBarStyle 。
class CCNavigationController: UINavigationController {
override var childForStatusBarHidden: UIViewController? {
return self.topViewController
}
override var childForStatusBarStyle: UIViewController? {
return self.topViewController
}
}

Preferred status bar style of view controller is ignored when in navigation controller

I'm writing an iOS App with multiple views. I've set the App to use ViewController-based status bar style, which allows me to use the following code
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
That worked like expected.
But then I've embedded the views in a navigation controller and connected a BarButtonItem with a showSegue. Since then the ViewController of the view switched to ignores the style settings and shows the default black status bar.
When you're in a navigation controller that will not get called. The navigation controller's preferredStatusBarStyle will be called. Try this along with your code:
extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return topViewController?.preferredStatusBarStyle ?? .default
}
}
There is a solution that is a bit more concise (and recommended by Apple):
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return topViewController
}
}

iOS set statusbar white, and hide in another ViewController

I need a white status bar for my main initial ViewController, but also be able to hide it in another ViewController. I can do one or the other, but not both at the same time. Any suggestions?
I set it to white by setting
View controller-based status bar appearance key to NO in the
Info.plist
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
And to hide it (on another ViewController presented modally) I do,
override func prefersStatusBarHidden() -> Bool {
return statusBarHidden
}
however it won't hide unless I remove the key I previously added to the Info.plist, but if I remove the key, then the status bar goes back to black.
EDIT-MY Solution:
The View controller classes were not working in my case because I have the main view controller embedded in a navigation controller, my fix was to override the same methods but for the navigation controller instead of the view controller in question.
extension UINavigationController {
public override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
If you add
View controller-based status bar appearance key to NO in the Info.plist
You should use [UIApplication setStatusBarHidden:withAnimation:] and [UIApplication setStatusBarStyle:animated:] to change to the statusbar's appearance. Your method won't be called
As it is said in apple's document:
UIViewControllerBasedStatusBarAppearance (Boolean - iOS) Specifies whether the status bar appearance is based on the style preferred by the view controller that is currently under the status bar. When this key is not present or its value is set to YES, the view controller determines the status bar style. When the key is set to NO, view controllers (or the app) must each set the status bar style explicitly using the UIApplication object.
UIViewControllerBasedStatusBarAppearance key is the same as View controller-based status bar appearance key.
If you don't add the key,your method should work. try add both methods in these two Viewcontrollers, and add some breakpoint to see they're called or override by mistake.
The code in Swift 3.0 Xcode 8.2 works for me:
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override open var prefersStatusBarHidden: Bool {
return true
}

Hiding the statusbar iOS

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

swift ios trying to change color of status bar on certain VC's

In my app I have 4 ViewController and in two of them I am changing the status bar from white to black like this:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = .LightContent
}
The problem is if I switch between two ViewController that both have the code above the status bar will first change to black which is right, but then it changes to white again when entering the other ViewController.
How can I keep the status bar white on certain ViewController's ?
Try to add the following method in your VC's. Use .default or .lightContent to change the status bar color. I tested using Xcode 8 and swift 3:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.default;
}
I created a new tabbed application with Xcode 7.3.1 and swift 2.3. I have two tabs with the classes associated FirstViewController and SecondViewController. In FirstViewController I added the following method:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.Default;
}
And In the SecondViewController, I changed the background to black and I added the following method:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent;
}
Finally, I added two buttons in the FirstViewController. One button present a controller modally and the other button present through push. When I presented the view modally the 'preferredStatusBarStyle' work but when I presented through push I need to add the following line of code:
self.navigationController?.navigationBar.barStyle = .Black
If you really don't want to override the delegate methods for preferredStatusBarStyle, you can still use:
UIApplication.sharedApplication().statusBarStyle = .LightContent
by removing:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = .LightContent
}
and let just let the status bar be set by whats happening in viewWillAppear. Obviously this is more prone to error, but if your navigation is relatively linear then it would be the simplest solution

Resources