首页 > 解决方案 > 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

标签: iosswiftstatusbar

解决方案


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
        }

推荐阅读