iOS13的presentViewController問題解決方案

UIViewController的modalPresentationStyle屬性,在iOS13之前默認值為UIModalPresentationFullScreen,iOS13中蘋果將默認值改為了UIModalPresentationAutomatic。

而這個萬惡的UIModalPresentationAutomatic,蘋果文檔只寫了一句話:

For most view controllers, UIKit maps this style to the UIModalPresentationPageSheet style, but some system view controllers may map it to a different style.

所以幾乎所有使用了custom presentViewController的頁面全部會變成這個樣紙:

image.png

為了保持iOS13與老版本的UI一致,最簡單的辦法就是全局將UIViewcontroller的modalPresentationStyle屬性改回UIModalPresentationFullScreen。

這里我沒太深入的思考,用runtime交換了下modalPresentationStyle的get方法,把我自己hack的get方法return出UIModalPresentationFullScreen就行了。

@implementation UIViewController (MDAdditions)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL sel = @selector(modalPresentationStyle);
        SEL swizzSel = @selector(swiz_modalPresentationStyle);
        Method method = class_getInstanceMethod([self class], sel);
        Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
        BOOL isAdd = class_addMethod(self, sel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
        if (isAdd) {
            class_replaceMethod(self, swizzSel, method_getImplementation(method), method_getTypeEncoding(method));
        }else{
            method_exchangeImplementations(method, swizzMethod);
        }
    });
}

- (UIModalPresentationStyle)swiz_modalPresentationStyle {
    return UIModalPresentationFullScreen;
}

@end

當然重寫presentViewController:方法之類的也可以,個人感覺區(qū)別不大。
有其他更好的改進意見歡迎提出。

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

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

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