Search code examples
iosswiftuiimagepickercontrolleruistatusbar

Changing Image Picker Preferred Status Bar Style swift


the status bar style for my application is white except when image picker controller is presented and I have already extend my UINavigationController but it doesn't seem to be working on any view present only on pushed views does anyone have solution??

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

I have also try this method , but the navigationController is a let and the preferredStatusBarStyle is read-only

   func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        viewController.navigationItem.title = "willShow"
        navigationController.preferredStatusBarStyle = UIStatusBarStyle.lightContent
    }

Solution

  • When you present something modally and you want it to determine the status bar style you need to set modalPresentationCapturesStatusBarAppearance = true

    For example:

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

    You'll also need to check if the current UINavigationController is a UIImagePickerController and return .lightContent from preferredStatusBarStyle as UIImagePickerController has a prefers the .default out of the box.

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