在 iOS13 中更改状态栏背景颜色

Nij*_*Nij 11 objective-c statusbar ios ios13

在 iOS13 中,我无法再更改状态栏的背景颜色,因为无法再使用“键值”访问状态栏。有没有人弄清楚这是怎么可能的,或者是否知道在 iOS13 的最终版本中是可能的?

我遇到了不同的建议,比如使用 UIApplications StatusBarView(在 xcode 11、beta 7 中不再可用)或使用 statusbarmanager。两者都不允许访问状态栏。

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
  statusBar.backgroundColor = <Some Color>
}
Run Code Online (Sandbox Code Playgroud)

我希望状态栏能够获得我需要的背景颜色。

小智 6

使用 UIStatusBarManager 的 Objective-C 版本:

UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
statusBar.backgroundColor = [UIColor whiteColor];
[[UIApplication sharedApplication].keyWindow addSubview:statusBar]
Run Code Online (Sandbox Code Playgroud)


Jas*_*our 6

代码适用于 Swift 5+ 和 iOS 13+

为了更改 StatusBar 背景颜色,我创建了 UIViewController 的基类,以便我可以在所有视图控制器中继承相同的代码。

在 UIViewController 扩展中添加了“statusBarColorChange()”。

extension UIViewController {

  func statusBarColorChange() {

    if #available(iOS 13.0, *) {

        let statusBar = UIView(frame: UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
        statusBar.backgroundColor = .appNavigationThemeColor
            statusBar.tag = 100
        UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)

    } else {

            let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
            statusBar?.backgroundColor = .appNavigationThemeColor

        }
    } }
Run Code Online (Sandbox Code Playgroud)

创建基类并在其他类中继承。

class BaseClass: UIViewController {

   override func viewWillAppear(_ animated: Bool) {
       self.statusBarColorChange()
   }    
}

class ChatListViewController: BaseClass {} 
Run Code Online (Sandbox Code Playgroud)

希望能有所帮助:)


小智 5

在您首选的 ViewController 上使用它

override func viewDidAppear(_ animated: Bool) {
    if #available(iOS 13, *)
    {
        let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
        statusBar.backgroundColor = UIColor.systemBackground
        UIApplication.shared.keyWindow?.addSubview(statusBar)
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以在“UIColor.systemBackground”中使用你的颜色

  • 调用 viewWillDisappear() 时如何删除 statusBar? (2认同)

Aju*_*mal 0

 let appearance = UINavigationBarAppearance()
 appearance.backgroundColor = .blue
 self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您。