由于升级Xcode适配iOS13,导致modalPresentationStyle 默认值变成了UIModalPresentationPageSheet,而我们APP里原本都是基于全屏去设计的,苹果这一修改,导致头部空出一片不说,还可能导致原本设计在底部的内容无法正常显示。
方案一:手动每次 present的时候,都去修改它的 modalPresentationStyle = UIModalPresentationFullScreen
当然这是最笨的方法
方案二:在需要persent的页面,init方法中,添加 self.modalPresentationStyle = UIModalPresentationFullScreen
但是找出这些页面,其实并不容易,也繁琐
方案三:利用UIViewController 分类的方法,替换掉 原来present方法,在每次present的时候,判断viewControllerToPresent.modalPresentationStyle == UIModalPresentationPageSheet 然后手动把 modalPresentationStyle改成 UIModalPresentationFullScreen
代码如下:
@implementation UIViewController (Present)
+ (void)load {
Method originAddObserverMethod = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
Method swizzledAddObserverMethod = class_getInstanceMethod(self, @selector(JF_presentViewController:animated:completion:));
method_exchangeImplementations(originAddObserverMethod, swizzledAddObserverMethod);
}
- (void)JF_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
if (@available(iOS 13.0, *)) {
if (viewControllerToPresent.modalPresentationStyle == UIModalPresentationPageSheet) {
viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
}
[self JF_presentViewController:viewControllerToPresent animated:flag completion:completion];
} else {
[self JF_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
}
这样的话,对于其他modalPresentationStyle,不做修改,保持原来的。全局把所有的UIModalPresentationPageSheet 替换成了UIModalPresentationFullScreen
以最小的修改量,完成最安全,最完善的需求,perfect!~