0

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
    }
beyowulf
  • 15,101
  • 2
  • 34
  • 40
Nouf
  • 733
  • 1
  • 11
  • 32

1 Answers1

1

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
}
beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • I dont understand why do I need to set rootViewController: MyViewController() when I want to show UIImagePicker? – Nouf Apr 06 '19 at 12:51
  • and using rootViewController with UIImagePicker will crash the app 'Pushing a navigation controller is not supported' – Nouf Apr 06 '19 at 12:57
  • That was just an example. You just need to set `modalPresentationCapturesStatusBarAppearance` whenever you want the presented view controller to determine the status bar style. If you're presenting a `UIImagePickerController`, set it on the instance of that that you're presenting. – beyowulf Apr 06 '19 at 14:52
  • I did but it still black " imagePicker.modalPresentationCapturesStatusBarAppearance = true " – Nouf Apr 06 '19 at 15:03