preferredStatusBarStyle removed in Swift 3?
Asked Answered
L

2

48

so I recently just updated to Xcode 8 and I just got this error for the following code.

The error is that the method doesn't override any method from it's superclasses.

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
}

enter image description here

I would really appreciate help, thanks!

Liverish answered 9/8, 2016 at 23:53 Comment(0)
G
146

In iOS 10, preferredStatusBarStyle is a property, not a method. So instead of overriding it with a func declaration as you've done, you override the getter with a var declaration:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

The Swift compiler's error message here could probably be better — since it seems to know your override func is supposed to cover a property, it could probably offer that as a fix-it. I'd recommend filing a bug to Swift open source.


Note: in iOS 12, you may also need to set the UIViewControllerBasedStatusBarAppearance (aka "View controller-based status bar appearance") flag in your Info.plist. And check your view controller hierarchy, as container view controllers like navigation and split view might not propagate this from their children.

Geminius answered 10/8, 2016 at 5:18 Comment(9)
@erkanyildiz yes and no: probably no one likes a language that old code stops working while a new version comes out; but in the other hand, most Swift 3 changes do make sense.Restitution
This doesn't seem to be working in iOS 12 and Swift 4. Any ideas? I'm not getting any errors, but the status bar is not changing either.Finger
Having the same problem with @Finger here. This code doesn't do anything with my status barAtwater
This code still gets called in iOS 12 — so there's nothing wrong with your Swift — but it's possible that the behavior of the underlying feature has changed. Further investigation needed.Geminius
You need to also: self.setNeedsStatusBarAppearanceUpdate()Waler
unfortunately this does not seem to work for me. I have the view controller inside of a tab bar, and other tabs do not have the issue, but this one does. No idea why.Palestra
turns out the nav controller wrapping the view controller needed to have its preferred content set... Otherwise the default for the navigation controller will control the status bar, and not the setting of the view controller it is embedding. So if this doesn't work, check if you are wrapped in a nav controller (etc.)Palestra
yea, wasn't "swifty enough"... smhOao
@Finger and JkAlombro are you sure it's not working? I thought so, too, but it was because I didn't realize what it was supposed to do. All it does is change the text in the status bar (in the Simulator, it's just "Carrier", the WiFi icon, the current time, and battery indicator) from Black to White. Nothing else. In any case, it's working for me today, using Xcode 10.2 and targeting Swift 12.2.Backandforth
F
7

The iOS 10.0 API Diffs shows that the UIViewController.preferredStatusBarStyle() -> UIStatusBarStyle method has been removed and that the UIViewController.preferredStatusBarStyle property has been added.


In a similar way, with Xcode 8, you can right-click on any UIViewController in your code, select Jump to Definition and then perform a search for preferredStatusBarStyle. You will discover that preferredStatusBarStyle now has the following declaration:

@available(iOS 7.0, *)
open var preferredStatusBarStyle: UIStatusBarStyle { get }

Therefore, the following code snippet shows how to override preferredStatusBarStyle with Xcode 8 / Swift 3:

override var preferredStatusBarStyle: UIStatusBarStyle {
    get {
        return .lightContent
    }
}

Or, in a much shorter and preferred style:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}
Foreknow answered 25/11, 2016 at 13:37 Comment(2)
I'm getting the following error "UIStatusBarStyle" is unavailable. Any ideas? (tvOS)Incase
Very helpful explanation. I'll be able to use on other things. Thank you.Gauss

© 2022 - 2024 — McMap. All rights reserved.