快速更改图像选取器首选状态栏样式



我的应用程序的状态栏样式是白色的,除非显示图像选择器控制器并且我已经扩展了我的UINavigationController但它似乎不适用于仅在推送视图上存在的任何视图有人有解决方案吗?

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .lightContent
    }
}

我也尝试过这种方法,但是导航控制器是一个让和 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并从preferredStatusBarStyle返回.lightContent,因为UIImagePickerController具有首选开箱即用的.default

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

最新更新