zoukankan      html  css  js  c++  java
  • iOS 13.4 & Xcode 11.4 采坑小记(重写系统get)

    最近更新了新系统,发现Modal样式设置的UIModalPresentationFullScreen失效了

    相信大家在适配iOS13 系统的时候,为了适配Modal默认样式发生变化( iOS13之前默认为UIModalPresentationFullScreen 13之后变为UIModalPresentationAutomatic)很多人是通过分类方法实现的。

    即:为UIViewController扩展该方法(本质是重写modalPresentationStyle属性的get方法),这样所有地方就直接生效了,不用一处一处修改,当然私有pod库中的还是要自己修改。

    /*
     Defines the presentation style that will be used for this view controller when it is presented modally. Set this property on the view controller to be presented, not the presenter.
     If this property has been set to UIModalPresentationAutomatic, reading it will always return a concrete presentation style. By default UIViewController resolves UIModalPresentationAutomatic to UIModalPresentationPageSheet, but system-provided subclasses may resolve UIModalPresentationAutomatic to other concrete presentation styles. Participation in the resolution of UIModalPresentationAutomatic is reserved for system-provided view controllers.
     Defaults to UIModalPresentationAutomatic on iOS starting in iOS 13.0, and UIModalPresentationFullScreen on previous versions. Defaults to UIModalPresentationFullScreen on all other platforms.
     */
    @property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle API_AVAILABLE(ios(3.2));
    // 适配iOS13
    - (UIModalPresentationStyle)modalPresentationStyle {
       //    适配iOS13系统下 RN弹窗内容被遮挡
        if ([self isKindOfClass:NSClassFromString(@"RCTModalHostViewController")]) {
            return UIModalPresentationOverCurrentContext;
        } else {
            return UIModalPresentationFullScreen;
        }
    }

    然后问题来了,分类中的 - (UIModalPresentationStyle)modalPresentationStyle 这个方法在Xcode 11.4 & iOS 13.4 环境下 不再执行了。

    至此找到问题根本原因是 Xcode 11.4 & iOS 13.4 环境下 分类重写系统级属性的get方法会失效。正常的系统级方法(如:- (void)viewWillAppear:(BOOL)animated;重写)还是会调用。

    解决办法:(目前最高效办法是1,若后续苹果不修复此BUG,可用方法2解决)

    1、使用低版本Xcode打包

    2、使用运行时方法替换(系统还是会调用这个方法,只不过不再调用我们重写的get方法,所以替换方法实现)

    + (void)swizzleInstanceSelector:(SEL)originalSelector withSelector:(SEL)swizzledSelector
    {
        Class class = [self class];
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (success)
        {
            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        }
        else
        {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    }
    
    + (void)load
    {    
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            
             [self swizzleInstanceSelector:@selector(modalPresentationStyle) withSelector:@selector(uiviewController_modalPresentationStyle)];
        });
    }
    
    - (UIModalPresentationStyle)uiviewController_modalPresentationStyle
    {
       //    适配iOS13系统下 RN弹窗内容被遮挡
        if ([self isKindOfClass:NSClassFromString(@"RCTModalHostViewController")]) {
            return UIModalPresentationOverCurrentContext;
        } else {
            return UIModalPresentationFullScreen;
        }
    }

    拓展:

    系统属性的get方法重写可能都会受到影响

  • 相关阅读:
    php操作zip压缩文件
    php图像处理(thinkphp框架有相对强大的图像处理功能)
    php实现希尔排序(总结)
    PHP glob() 函数详解
    php资源类型变量
    dump var_dump print print_r的区别
    php实现遍历文件目录
    php常用函数
    php全局变量的使用
    php函数按地址传递参数(php引用)
  • 原文地址:https://www.cnblogs.com/lijianyi/p/12607377.html
Copyright © 2011-2022 走看看