【问题标题】:How to changes status bar colour of second UIWindow?如何更改第二个 UIWindow 的状态栏颜色?
【发布时间】:2019-10-17 10:05:48
【问题描述】:

我在我的应用中添加了第二个窗口,用于在用户点击远程推送通知时显示特定控制器。

func navigateThroughPushnotification(controller: UINavigationController) {
    let window = UIWindow(frame: UIScreen.main.bounds)
    let vc = UIViewController()
    vc.view.backgroundColor = .clear
    window.rootViewController = vc
    window.windowLevel = UIWindow.Level.alert + 1
    window.makeKeyAndVisible()
    if let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
        statusBar.backgroundColor = kAppDefaultColor
    }
    vc.present(controller, animated: true, completion: nil)
}

问题是我也想更改此窗口的状态栏颜色,但我用来更改主窗口颜色的代码这次不起作用。

guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return true }
    statusBar.backgroundColor = kAppDefaultColor

现在这段代码在主窗口中运行得很好。 请有人帮助检测应该做些什么来改变第二个窗口的颜色。 提前致谢

【问题讨论】:

  • 你想改变整个应用程序状态栏的颜色吗?
  • @BhumeshPurohit 我已经在整个应用程序中更改状态栏颜色,但是在这种情况下创建另一个窗口时会失败

标签: ios swift uistatusbar


【解决方案1】:

仅适用于 iOS 7 及更高版本

首先进入你应用的Info.plist,将View controller-based status bar appearance设置为YES。

在您的每个视图控制器中覆盖 preferredStatusBarStyle 之后。

例如:

override var preferredStatusBarStyle: UIStatusBarStyle {     
      return .lightContent
}

如果您有 preferredStatusBarStyle 根据视图控制器内部的更改返回不同的首选状态栏样式,那么您希望在状态更改时调用 setNeedsStatusBarAppearanceUpdate()。

【讨论】:

  • 我什么都做了。即在 info.plist 中定义了密钥,并且对于初始 Window 工作正常。但是当我在这种情况下收到推送时定义了另一个,这是行不通的
猜你喜欢
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多