更改图像选择器首选状态栏样式 swift

Changing Image Picker Preferred Status Bar Style swift

我的应用程序的状态栏样式是白色的,除非出现图像选择器控制器并且我已经扩展了我的 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 和 return .lightContent 来自 preferredStatusBarStyleUIImagePickerController有一个更喜欢开箱即用的 .default

open override var preferredStatusBarStyle: UIStatusBarStyle {
    if self is UIImagePickerController {
        return .lightContent
    }
    return topViewController?.preferredStatusBarStyle ?? .lightContent
}