iOS13 persent 適配

由于升級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!~

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容