1.iOS 13以后modalPresentationStyle的变化
iOS 13系统之前 modalPresentationStyle 的默认值是UIModalPresentationFullScreen = 0,这时候模态推出的控制器是全屏展示的。但iOS 13系统以后modalPresentationStyle的默认值变成了UIModalPresentationAutomatic API_AVAILABLE(ios(13.0)) = -2,这时候如果你还是采用默认的值,那么模态推出的控制器就不是全屏展示的。iOS 13 的 presentViewController 默认有视差效果,模态出来的界面现在默认都下滑返回。
2.使用hook方法实现全局控制。
如果是以前的老项目中使用模态,但没有设置modalPresentationStyle的值,那么要不想每个界面都设置下,可以使用hook方法。你需要新建一个UIViewControl的分类,然后重写load方法,代码如下:
UIViewController+ChangeUI.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (ChangeUI)
@end
NS_ASSUME_NONNULL_END
UIViewController+ChangeUI.m
//自动调的,不需要单独在导入头文件调用
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
swizzling_exchangeMethod([self class], @selector(presentViewController:animated:completion:), @selector(myPresentViewController:animated:completion:));
});
}
- (void)myPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
//设置满屏,不需要小卡片
if(@available(iOS 13.0, *)) {
viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
}
[self myPresentViewController:viewControllerToPresent animated:flag completion:completion];
}
上面代码我只列举了部分,详细的可以看下我写的 demo ,如果写的有什么问题,欢迎下方评论