iOS13的presentViewController問題解決方案

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

而這個(gè)萬惡的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的頁面全部會(huì)變成這個(gè)樣紙:

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

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • /* UIViewController is a generic controller base class th...
    DanDanC閱讀 1,843評(píng)論 0 2
  • 前言: iOS13的API的變動(dòng)和適配問題,在一段時(shí)間內(nèi)可能會(huì)有不同的問題和方式出現(xiàn),會(huì)持續(xù)更新,如果您有好的建議...
    31313_iOS閱讀 26,552評(píng)論 40 62
  • 更好的閱讀體驗(yàn),請(qǐng)到個(gè)人博客閱讀: iOS中的系統(tǒng)轉(zhuǎn)場 請(qǐng)忽略標(biāo)題,??,本文記錄的是對(duì)下圖所示的Kind, Pre...
    CaryaLiu閱讀 2,375評(píng)論 0 1
  • 寫故事的人總能夠用自己的文字救贖看故事的人 阿甲給我說她看完《那不勒斯四部曲》后哭了一場,決定斷絕心中的念...
    水馨閱讀 2,376評(píng)論 2 5
  • 風(fēng)景,風(fēng)景,新的風(fēng)景,我要呼吸這風(fēng)景。 春蘭,夏竹,秋菊,冬梅,都是我的最愛! 寫作,再美好不過的職業(yè)。幾乎集自由...
    軍范律政閱讀 18,634評(píng)論 233 938