简体   繁体   English

iOS Swift 4 状态栏 - 禁用半透明

[英]iOS Swift 4 Status bar - disable Translucent

I am trying to get the status bar of my iOS (webView) app not Translucent.我试图让我的 iOS (webView) 应用程序的状态栏不是半透明的。

I tried this inside func viewDidLoad():我在 func viewDidLoad() 中试过这个:

self.navigationController?.navigationBar.isTranslucent = false

And this in the appDelegate:这在 appDelegate 中:

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

This is what I am getting when scrolling the page..这就是我滚动页面时得到的.. 在此处输入图片说明

You cannot change those properties for the status bar. 您不能更改状态栏的那些属性。 You can only set, .default, .lightContent. 您只能设置.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 那可以进入ViewController viewDidLoad()方法

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: 将以下代码粘贴到您的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. 如果它不起作用,请检查您的Info.plist是否状态栏外观基于ViewController。 Add this: 添加:

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

Swift 5.1 iOS 13.0 Just in case for the current deprecations... Swift 5.1 iOS 13.0以防万一当前不推荐使用...

 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
        }

当我尝试将视图从上方滑入屏幕时,这为我修复了它,但在动画时没有在状态栏上看到它:

view.clipsToBounds = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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