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

标签 ios swift uiimagepickercontroller uistatusbar

我的应用程序的状态栏样式是白色的,除非出现图像选择器 Controller 并且我已经扩展了我的 UINavigationController 但它似乎不适用于仅在推送 View 上出现的任何 View 有人有解决办法吗??

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 并从 preferredStatusBarStyle 返回 .lightContent > 因为 UIImagePickerController 更喜欢开箱即用的 .default

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

关于ios - 快速更改图像选择器首选状态栏样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55548215/

相关文章:

ios - 对符合协议(protocol)的所有对象调用方法

ios - 将值传递给与 uiview 关联的类并让 draw 识别值而不是默认类值

ios - ImagePicker 选取图像后 CollectionView 图像不更新

ios - UIBarButton 项目不可见

ios - DBAccess:是否支持长长值?

ios - UITextField不能随着键盘启动移动到正确的位置

iphone - 邮件应用程序如何在编辑模式下禁用 UISearchBar?

swift - 返回上一个 View Controller 时的 UIImage 问题

ios - UIScrollView:缩放以适应屏幕宽度

swift - 如何在 SwiftUI 中从图库中选择图像