DJack
DJack

Reputation: 639

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

Upvotes: 8

Views: 6621

Answers (4)

AmitP
AmitP

Reputation: 5483

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

Upvotes: 1

Marlhex
Marlhex

Reputation: 1987

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
        }

Upvotes: 9

Galo Torres Sevilla
Galo Torres Sevilla

Reputation: 1575

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

Upvotes: 10

alanpaivaa
alanpaivaa

Reputation: 2089

I don't think you can play with status bar translucency. However, you might want to hide it. Paste the following code in your ViewController:

override var prefersStatusBarHidden: Bool {
  return true
}

If it doesn't work, check in your Info.plist if your status bar appearance is ViewController based. Add this:

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

Upvotes: 0

Related Questions