美文网首页iOS常用
iOS13的presentViewController问题解决方

iOS13的presentViewController问题解决方

作者: wsxiaoluob | 来源:发表于2019-10-01 02:34 被阅读0次

    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:方法之类的也可以,个人感觉区别不大。
    有其他更好的改进意见欢迎提出。

    相关文章

      网友评论

        本文标题:iOS13的presentViewController问题解决方

        本文链接:https://www.haomeiwen.com/subject/aldyuctx.html