码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 13.4 & Xcode 11.4 采坑小记(重写系统get)

时间:2020-03-31 19:00:21      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:anim   使用   add   patch   rev   nonatomic   provided   default   not   

最近更新了新系统,发现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、使用低版本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方法重写可能都会受到影响

iOS 13.4 & Xcode 11.4 采坑小记(重写系统get)

标签:anim   使用   add   patch   rev   nonatomic   provided   default   not   

原文地址:https://www.cnblogs.com/lijianyi/p/12607377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!