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

时间:2019-04-06 10:34:57

标签: ios swift uiimagepickercontroller uistatusbar

我的应用程序的状态栏样式为白色,除非显示了图像选择器控制器并且我已经扩展了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
    }

1 个答案:

答案 0 :(得分:1)

当您以模态方式呈现某些内容并希望其确定状态栏样式时,您需要设置modalPresentationCapturesStatusBarAppearance = true

例如:

let navigationController = UINavigationController(rootViewController: MyViewController())
navigationController.modalPresentationCapturesStatusBarAppearance = true
present(navigationController, animated: true)

您还需要检查当前的UINavigationController是否为UIImagePickerController,并从.lightContent返回preferredStatusBarStyle,因为UIImagePickerController偏爱{{ 1}}开箱即用。

.default