Since UIApplication.shared.setStatusBarStyle(.default, animated: true) is deprecated from IOS9 is it possible to change status bar style with animation on push? I cannot find any description in docs.
It's now a variable you have to override:
override var preferredStatusBarStyle: UIStatusBarStyle
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation
Depending on when you update the status bar, you might also have to call setNeedsStatusBarAppearanceUpdate()
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file.
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.
To tackle the animation part the way I made it transition smoothly between .lightContent and .default was to use something similar to below each time you change it.
UIView.animate(withDuration: 0.2) {
self.setNeedsStatusBarAppearanceUpdate()
}
Place that in your VC you want the status bar to animate and you'll have a nice smooth animation.
override var preferredStatusBarStyle: UIStatusBarStyle {
return lightStatusBar ? .lightContent : .default
}
Above is a snippet of me changing the content based on a condition I have.
Related
I try to put my Status Bar in Light Content.
The problem is that I have set View controller-based status bar
appearance to YES .
In my ViewController I put :
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
I also try to put this in my AppDelegate :
application.statusBarStyle = .lightContent
After thats I still have a Dark (black) Status Bar.
ios 10 and swift 3
Change in info.plist the row View controller-based status bar appearance and set it to NO
Change in appDelegate.swift in didFinishLaunchingWithOptions
UIApplication.shared.statusBarStyle = .lightContent
In perticular viewcontroller use
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
In your ViewController's viewDidLoad method try calling
self.setNeedsStatusBarAppearanceUpdate()
didFinishLaunching Method in AppDelegate Class Single line code.
application.statusBarStyle = .lightContent
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.
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.
I'm trying to implement a button that changes the background of my app from white to black along with the colour of the status bar. Right now I have
#IBAction func buttonTapped(_ sender: Any) {
self.view.backgroundColor = UIColor.black
UIApplication.shared.statusBarStyle = .lightContent
}
This doesn't seem to work. Does anyone know of a way of changing the colour of the status bar font without reloading the entire view?
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file.
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.
Since setStatusBarStyle was deprecated in iOS 10, is there a way to change my status bar from white to black or vice versa while doing something like scrolling through a scroll view?
I see the preferredStatusBarStyle property on UIViewController but I'd like something more granular that I can control while a user uses the view controller itself.
if you override preferredStatusBarStyle and then call setNeedsStatusBarAppearanceUpdate() when you need, that should work the same way.
class MyViewController: UIViewController {
var currentStyle = UIStatusBarStyle.default
override var preferredStatusBarStyle: UIStatusBarStyle {
return currentStyle
}
// ...
// here are the actions that change the status bar
func myFunction(){
// ...
// condition that would determine the preferred style
currentStyle = .lightContent
setNeedsStatusBarAppearanceUpdate()
}
}
I'm finding it hard to change the status bar style programatically.
I see how to statically set it for each ViewController using a combo of (in ViewController.swift):
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.default
}
and (in info.plist):
View controller-based status bar appearance = YES
...
I'm looking to change it whenever I want!
Found the answer after quite a lot of digging!
Set (in info.plist):
View controller-based status bar appearance = NO
and remove the (in ViewController.swift):
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.default
}
...
Now you can use (in ViewController.swift):
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
And, to initially set the style for each ViewController, use viewDidAppear:
override func viewDidAppear(_ animated: Bool) {
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false)
}
swift 3
1.Change in info.plist the row View controller-based status bar appearance and set it to NO
2.Change in appDelegate.swift in didFinishLaunchingWithOptions
UIApplication.shared.statusBarStyle = .lightContent
Store the status bar style as a property in your view controller:
var statusBarStyle: UIStatusBarStyle = .default
And then implement preferredStatusBarStyle in the same view controller:
override var preferredStatusBarStyle: UIStatusBarStyle {
return statusBarStyle
}
Then when you change statusBarStyle make sure to also call setNeedsStatusBarAppearanceUpdate. The preferredStatusBarStyle method is automatically called when the view appears/disappears, but if you change the status bar style while your view is visible, you have to tell the view controller the status bar appearance needs updating.
Note you still need to make the changes to the plist, and if your view controller is in a navigation controller, you may need to handle the status bar changes there instead (via a UINavigationController subclass, for example).
UIApplication.shared.setStatusBarStyle(…) was deprecated in iOS 9.0, so don't use that.
I'm currently developing an app (in Swift 3) on Xcode 8 beta, for iOS 10.
What I want to achieve is to change status bar style within a view controller at run time, for changing the theme from daytime theme to night theme.
I've found out that the method I used to use when I was developing another app in the past was deprecated, as shown here on the API reference.
However, preferredStatusBarStyle won't work here since I would like to change it within a single view controller.
Can anybody think of other ways to perform this?
Thanks in advance
EDIT:
To be clear, what I want to do is to change the style when the view controller is already on screen.
You can create a statusBarStyle variable that when changed updates the status bar appearance. If you only want this to affect one controller, simply reverse the effect when the Controller will or did disappear.
var statusBarStyle: UIStatusBarStyle = .lightContent {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return statusBarStyle
}
The above solution will override the previous controller's status bar style before the controller appears. If you want to change the status bar style when the controller appears, try this:
var statusBarStyle: UIStatusBarStyle? {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return statusBarStyle ?? super.preferredStatusBarStyle
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
statusBarStyle = .lightContent
}
In your info.plist, add the UIViewControllerBasedStatusBarAppearance key with a value of false.
Then, in your viewController when switching to your night theme:
UIApplication.shared.statusBarStyle = .lightContent
To go back to black:
UIApplication.shared.statusBarStyle = .default