Swift 3.0 - how to hide status bar after calling U

2020-02-14 04:20发布

Happy New Year!

I want to hide status bar after calling UIImagePickerController on iOS 10.2. You know there are already several questions about it with some answers, but I'm afraid the answers don't work for Swift 3.0.

The suggested answers are:

  • extend UINavigationController to override preferStatusBarHidden, because UIImagePickerController is a subclass of UINavigationController.

So I tried:

extension UINavigationController{
    open override var prefersStatusBarHidden: Bool{
        return true
    }
}
  • extend UIImagePickerController to override prefersStatusBarHidden.

So I tried:

extension UIImagePickerController{
    open override var prefersStatusBarHidden: Bool{
        return true
    }
}
  • create and use a subclass of UIImagePicker

So I tried:

class MyImagePickerController: UIImagePickerController{
    override var prefersStatusBarHidden: Bool{
        return true
    }
}

But, unfortunately, none of the above solutions seems working for me. I'd like iOS developers who are proficient in Swift 3.0 to let me know how to hide status bar after calling UIImagePickerController.

Thank you for your interest and time!

3条回答
The star\"
2楼-- · 2020-02-14 04:54

The status bar can be hidden and show when presenting view controller UIImagePickerController swift 4+

picker.dismiss(animated: true, completion: {
    if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
        statusBar.isHidden =  true
    }
})

picker.dismiss(animated: true, completion: {
    if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
        statusBar.isHidden =  false
    }
})
查看更多
淡お忘
3楼-- · 2020-02-14 04:59

You are adding the delegate method method of UINavigationControllerDelegate like below.

class PersonalInfoVC: UIViewController, UIImagePickerControllerDelegate , UINavigationControllerDelegate{

Adding the delegate and hide the status bar in it.

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool){
    UIApplication.shared.isStatusBarHidden = true
}
查看更多
地球回转人心会变
4楼-- · 2020-02-14 05:03

The status bar can be permanently hidden with the following extension to UIImagePickerController :

extension UIImagePickerController {
    open override var childViewControllerForStatusBarHidden: UIViewController? {
        return nil
    }

    open override var prefersStatusBarHidden: Bool {
        return true
    }
}

This is working for Swift 3, on iOS 10.

查看更多
登录 后发表回答