问题
I have a custom UIPresentationController and overrides frameOfPresentedViewInContainerView for a custom viewController presentation. Everything works fine, except for the status bar.
I do not want the status bar to change appearance at all – it should just stay however it looked before. Now the Apple Documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instp/UIViewController/modalPresentationCapturesStatusBarAppearance says if modalPresentationStyle is not UIModalPresentationFullScreen OR modalPresentationCapturesStatusBarAppearance is NO, i should be fine and the status bar should not change.
With this code:
- (BOOL)prefersStatusBarHidden {
NSLog(
@"prefersStatusBarHidden was called %d %ld",
self.modalPresentationCapturesStatusBarAppearance,
(long)self.modalPresentationStyle
);
return YES;
}
I can see that prefersStatusBarHidden is called, even if modalPresentationCapturesStatusBarAppearance is NO (displays as 0) and modalPresentationStyle is UIModalPresentationCustom (displays as 4).
Obviously, that's the reason the status bar changes when presenting the viewController.
But why?
My thought on this is that iOS thinks that the viewController is presented in fullscreen even though it is not.
I discovered UIPresentationController's property shouldPresentInFullscreen – it returns YES by default. Returning NO doesn't help at all, so i don't understand what that property even does... It has literally no effect. The same applies to the presentationStyle property – I don't see any effect when changing it. I would have expected the presentationStyle property to be "redirected" to the viewControllers modalPresentationStyle property, but that stays at UIModalPresentationCustom, which it has to be to initiate the custom presentation in the first place.
So, my questions are: Does anybody know how to just keep the status bar as it is with a custom UIPresentationController – and can somebody explain the shouldPresentInFullscreen and presentationStyle properties?
Thank you! :)
回答1:
Try implementing childViewControllerForStatusBarStyle: and return nil for it in the class invoking your UIPresentationController, usually a UINavigationController.
This is what I do in Swift when I don't want a child VC to interfere with my wisely chosen Status Bar Style:
override func childViewControllerForStatusBarStyle() -> UIViewController? {
return nil // ignore childs and let this Navigation Controller handle the StatusBar style
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent // or .Default depending on your Style
}
This requires iOS8 and newer and is only usable if you are setting the key UIViewControllerBasedStatusBarAppearance
in your Info.plist
to YES
.
Bonus: If this does not help in the caller, use it in the shown Ccontroller. I checked my projects, in one of them it's also in the NavigationController shown as PopOver and working fine as of today.
来源:https://stackoverflow.com/questions/31660920/how-to-use-modalpresentationcapturesstatusbarappearance-no-with-a-custom-uipre