Why is the status bar sliding back of 20 points my

2019-07-20 06:19发布

问题:

There appears to be a bug when UIImagePickerController gets called. The status bar shows up even it shouldn't.

To workaround that am using subclassing it:

class MyImagePickerController: UIImagePickerController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.setNeedsStatusBarAppearanceUpdate()
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    override func childViewControllerForStatusBarHidden() -> UIViewController? {
        return nil;
    }
}

and I use this code show up the Photo Library:

let picker = MyImagePickerController()

    picker.allowsEditing = false
    picker.sourceType = .SavedPhotosAlbum
    picker.modalPresentationStyle = .Popover
    self.presentViewController(picker, animated: true, completion: nil)
    picker.popoverPresentationController?.sourceRect = CGRectMake(0,0,0,0)
    picker.popoverPresentationController?.sourceView = self.view

However, the status bar gets hidden but it slides an image connected to the view via a constraint down of about 20 points. How can I fix that?

回答1:

I managed to fix this for my case by taking control of the nav bar in the imagePickerController. This may work for you, but it will depend on the precise context of your problem (i.e. state of view hierarchy before imagePicker is shown).

As with my previous solution, you subclass UIImagePickerController. This is in turn a subclass of UINavigationController, so you can get at it's navbar.

class WNImagePickerControllerSwift: UIImagePickerController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.setNeedsStatusBarAppearanceUpdate()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.setNavBar()
    }

    override func prefersStatusBarHidden() -> Bool {
        self.setNavBar()
        return true
    }

    override func childViewControllerForStatusBarHidden() -> UIViewController? {
        return nil;
    }

    func setNavBar() -> Void {
        self.setNavBar(65)
    }


    func setNavBar(height: CGFloat) -> Void {
        var frame = self.navigationBar.frame;
        frame.size.height = height;
        self.navigationBar.frame = frame;
    }

}

setNavBar has to be called in precisely those two places - once before the animation transition when prefersStatusBarHidden is invoked, and once again after the transition. It won't work if you call it directly in viewWillAppear.

Anyway it's worth a try, playing around with that magic number 65 to suit the navbar height that you are after.



回答2:

I was struggling with this 20 offset while editing image. It seems like a bug to me as well.

The only thing that works for me is that in info.list, set View controller-based status bar appearance to YES. Check if you have this set.

Then if you want to modify status bar in some of the viewcontrollers, go to every view controller needed to change it.