简体   繁体   中英

How to remove status bar transparency in IOS App

I want to remove status bar transparency在此处输入图像描述 here what i have tried so far:

class ViewController: UIViewController {
    
    @IBOutlet var webview: WKWebView!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let statusBarFrame = UIApplication.shared.statusBarFrame
        let statusBarView = UIView(frame: statusBarFrame)
        self.view.addSubview(statusBarView)
        statusBarView.backgroundColor = .green        
    }
    
}

Add subview on top of the status bar using statusBarManager .

In ViewController:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let statusBarFrame = view.window?.windowScene?.statusBarManager?.statusBarFrame {
            let statusBarView = UIView(frame: statusBarFrame)
            view.addSubview(statusBarView)
            statusBarView.backgroundColor = .green
        }
    }
}

Else in SceneDelegate:

let statusBarFrame = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame
let statusBarView = UIView(frame: statusBarFrame)
view.addSubview(statusBarView)
statusBarView.backgroundColor = .green

Do not remove it. Do not want to remove it. The status bar is transparent. Accept the facts and work with them, not against them.

If you want the status bar area to look green, put something green behind it. It would be better, however, to use a navigation controller and navigation bar, which automatically extends its drawing behind the status bar.

Use this code to change background for status bar

if (@available(iOS 13, *)) {
    let statusBarView =  UIView()
    statusBarView.frame = UIApplication.shared.statusBarFrame
    statusBarView.backgroundColor = UIColor.green
    UIApplication.shared.keyWindow?.addSubview(statusBarView)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM