I have gone around and around on this. I just want to hide the status bar on devices smaller than the iPhone 6. This answer is great but the code is now deprecated and throws an error.
I followed all the advice on this post (which was very helpful), and I have working code, but I have to copy and paste onto every view controller. That seems like a bad idea. It certainly doesn't abide by the DRY method.
Here is my code that works for a single view controller:
class Step1SplashVC: UIViewController {
var hideStatusBar: Bool = false
override var prefersStatusBarHidden: Bool { return true }
override func viewDidLoad() {
super.viewDidLoad()
let screenSize = Int(UIScreen.main.bounds.width)
print("screen size:", screenSize)
if screenSize <= 320 {
print("device is too small. need to hide status bar")
hideStatusBar = true
setNeedsStatusBarAppearanceUpdate()
}
My question is, how do I refactor this so that I'm not copying and pasting to every view controller in my project (I have about 35 view controllers in all)?
I tried creating an extension for UIViewController, but it kept throwing an error.
Here is the bad code:
extension UIViewController {
private struct Holder {
static var hideStatusBar: Bool = false
}
var screensize: Bool {
get {
return Int(UIScreen.main.bounds.width) <= 320
}
set {
Holder.hideStatusBar = true
}
}
override var prefersStatusBarHidden: Bool { return true }
}
The error I get is:
Property does not override any property from its superclass
So my efforts at creating an extension are not working.
How can I hide the status bar for smaller devices without having to copy and paste code onto every view controller?
Thanks!!