Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift 4 Status bar - disable Translucent

I am trying to get the status bar of my iOS (webView) app not Translucent.

I tried this inside func viewDidLoad():

self.navigationController?.navigationBar.isTranslucent = false

And this in the appDelegate:

    UINavigationBar.appearance().isTranslucent = false
    UINavigationBar.appearance().backgroundColor = .white

This is what I am getting when scrolling the page.. enter image description here

like image 476
DJack Avatar asked Feb 02 '19 18:02

DJack


3 Answers

You cannot change those properties for the status bar. You can only set, .default, .lightContent. But if you want you can probably place a view underneath of it, which is not translucent and has a background color. Something like this:

let statusBarFrame = UIApplication.shared.statusBarFrame
let statusBarView = UIView(frame: statusBarFrame)
self.view.addSubview(statusBarView)
statusBarView.backgroundColor = .green

That can go in you viewDidLoad() method of the ViewController

like image 194
Galo Torres Sevilla Avatar answered Oct 31 '22 20:10

Galo Torres Sevilla


Swift 5.1 iOS 13.0 Just in case for the current deprecations...

 if #available(iOS 13.0, *) {

            let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
            let statusBarFrame = window?.windowScene?.statusBarManager?.statusBarFrame

            let statusBarView = UIView(frame: statusBarFrame!)
            self.view.addSubview(statusBarView)
            statusBarView.backgroundColor = .green
        } else {
            //Below iOS13
            let statusBarFrame = UIApplication.shared.statusBarFrame
            let statusBarView = UIView(frame: statusBarFrame)
            self.view.addSubview(statusBarView)
            statusBarView.backgroundColor = .green
        }
like image 21
Marlhex Avatar answered Oct 31 '22 19:10

Marlhex


This fixed it for me when trying to slide a view from above into the screen, without seeing it on status bar while it animates:

view.clipsToBounds = true
like image 42
AmitP Avatar answered Oct 31 '22 20:10

AmitP