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

问题描述 投票:0回答:1

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

当您以模态方式呈现某些内容并且希望它确定状态栏样式时,您需要设置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
}
© www.soinside.com 2019 - 2024. All rights reserved.