iOS 13.4 & Xcode 11.4 采坑小记(重写系统get)
Posted 小菜看代码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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、使用低版本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)的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 11.4 Objective-C 语言的快速帮助,而不是 swift 语言,用于带有 Cocoapods 的 firebase API(iOS 13.4 应用程序)