iOS 13.0 + presentViewController 模态全屏适配解决方案

在iOS 13.0 之前,模态显示视图默认是全屏,但是iOS 13.0 之后,默认是Sheet卡片样式的非全屏,即:

之前,modalPresentationStyle值默认为:UIModalPresentationFullScreen;

之后,modalPresentationStyle默认值为:UIModalPresentationAutomatic;

typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationFormSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationCurrentContext API_AVAILABLE(ios(3.2)),
    UIModalPresentationCustom API_AVAILABLE(ios(7.0)),
    UIModalPresentationOverFullScreen API_AVAILABLE(ios(8.0)),
    UIModalPresentationOverCurrentContext API_AVAILABLE(ios(8.0)),
    UIModalPresentationPopover API_AVAILABLE(ios(8.0)) API_UNAVAILABLE(tvos),
    UIModalPresentationBlurOverFullScreen API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios) API_UNAVAILABLE(watchos),
    UIModalPresentationNone API_AVAILABLE(ios(7.0)) = -1,
    UIModalPresentationAutomatic API_AVAILABLE(ios(13.0)) = -2,
};

解决方案:
第一种:在每个方法中添加/修改控制器属性值modalPresentationStyle为UIModalPresentationFullScreen即可,如:

        vc.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:vc animated:YES completion:nil];

第二种:写一个Category,利用OC运行时(Runtime)特性做全局替换修改,避免遗漏某个页面,同时也能修改第三方代码中的模态显示,原理就是在运行时检查方法,然后做IMP交互,让方法重载,执行自定义代码,全部代码如下:

#import 
 
NS_ASSUME_NONNULL_BEGIN
 
@interface UIViewController (FullScreenModal)
 
@end
 
NS_ASSUME_NONNULL_END


#import "UIViewController+ FullScreenModal.h"
#import 
 
@implementation UIViewController (FullScreenModal)
 
+ (void)load{
    [super load];
    
    SEL originalSel = @selector(presentViewController:animated:completion:);
    SEL overrideSel = @selector(override_presentViewController:animated:completion:);
    
    Method originalMet = class_getInstanceMethod(self.class, originalSel);
    Method overrideMet = class_getInstanceMethod(self.class, overrideSel);
    
    method_exchangeImplementations(originalMet, overrideMet);
}
 
#pragma mark - Swizzling
- (void)override_presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL) animated completion:(void (^ __nullable)(void))completion{
    if(@available(iOS 13.0, *)){
        if (viewControllerToPresent.modalPresentationStyle ==  UIModalPresentationPageSheet){
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
    }
    
    [self override_presentViewController:viewControllerToPresent animated: animated completion:completion];
}
                                  
 
@end


你可能感兴趣的:(iOS 13.0 + presentViewController 模态全屏适配解决方案)