Changing status bar style mid-scroll - ios

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()
}
}

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
}
}

Change status bar style with animation

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.

Swift 3 hide status bar after ViewController init

I have a UIViewcontroller sub-class with the following properties:
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override var prefersStatusBarHidden: Bool {
return false
}
And would like to change preferStatusBarHidden to true based on method calls, after view controller init. I am unable to change it to true (it's get only).
I think I'm missing something silly here...
Edit:
This is not a duplicate from other hide status bar questions because it involves a property override and computation to determine the property after the view controller has already been presented.
Add your logic for when the status bar should be hidden inside the getter for prefersStatusBarHidden (where you currently just have return false). Then, when you need to trigger an update, call setNeedsStatusBarAppearanceUpdate().

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
}
}

setStatusBarStyle:animated: deprecated

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

Resources