我的应用程序的状态栏样式是白色的,除非呈现图像选择器控制器并且我已经扩展了我的UINavigationController
但它似乎没有工作任何只在推送视图上出现的视图是否有人有解决方案?
extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return topViewController?.preferredStatusBarStyle ?? .lightContent
}
}
我也试过这个方法,但是navigationController是一个let,而preferredStatusBarStyle
是只读的
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.navigationItem.title = "willShow"
navigationController.preferredStatusBarStyle = UIStatusBarStyle.lightContent
}
当您以模态方式呈现某些内容并且希望它确定状态栏样式时,您需要设置modalPresentationCapturesStatusBarAppearance = true
例如:
let navigationController = UINavigationController(rootViewController: MyViewController())
navigationController.modalPresentationCapturesStatusBarAppearance = true
present(navigationController, animated: true)
你还需要检查当前的UINavigationController
是否是UIImagePickerController
并从.lightContent
返回preferredStatusBarStyle
,因为UIImagePickerController
更喜欢开箱即用的.default
。
open override var preferredStatusBarStyle: UIStatusBarStyle {
if self is UIImagePickerController {
return .lightContent
}
return topViewController?.preferredStatusBarStyle ?? .lightContent
}