由于升級Xcode適配iOS13,導致modalPresentationStyle 默認值變成了UIModalPresentationPageSheet,而我們APP里原本都是基于全屏去設計的,蘋果這一修改,導致頭部空出一片不說,還可能導致原本設計在底部的內(nèi)容無法正常顯示。
方案一:手動每次 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!~