【问题标题】:Changing Image Picker Preferred Status Bar Style swift快速更改图像选择器首选状态栏样式
【发布时间】:2019-04-06 10:34:57
【问题描述】:

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

【问题讨论】:

    标签: ios swift uiimagepickercontroller uistatusbar


    【解决方案1】:

    当您以模态方式呈现某些内容并希望它确定您需要设置的状态栏样式时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
    }
    

    【讨论】:

    • 我不明白为什么要显示 UIImagePicker 时需要设置 rootViewController: MyViewController()?
    • 并使用带有 UIImagePicker 的 rootViewController 会使应用程序崩溃“不支持推送导航控制器”
    • 这只是一个例子。只要您希望呈现的视图控制器确定状态栏样式,您只需设置modalPresentationCapturesStatusBarAppearance。如果您正在展示 UIImagePickerController,请将其设置在您正在展示的实例上。
    • 我做了,但它仍然是黑色的 " imagePicker.modalPresentationCapturesStatusBarAppearance = true "
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2022-11-18
    • 2017-12-13
    相关资源
    最近更新 更多