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

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

之前,modalPresentationStyle值默认为:UIModalPresentationFullScreen;

之后,modalPresentationStyle默认值为:UIModalPresentationAutomatic;

解决方案:

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

-(void)openTypeDetailVC:(int)row{
    ITQuestionDetailViewController *detailVC = [[ITQuestionDetailViewController alloc] init];
    detailVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:detailVC animated:YES completion:nil];
}

对比效果图如下:

  

 

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

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (ITModal)

@end

NS_ASSUME_NONNULL_END
#import "UIViewController+ITModal.h"
#import <objc/runtime.h>

@implementation UIViewController (ITModal)

+ (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)flag completion:(void (^ __nullable)(void))completion{
    if(@available(iOS 13.0, *)){
        if (viewControllerToPresent.modalPresentationStyle ==  UIModalPresentationPageSheet){
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
    }
    
    [self override_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
                                  

@end

只需将本Category类放入工程即可解决。

对比效果图如下(解决三方平台无法修改源代码问题):

 

  

 

至此,iOS 13.0 的模态全屏适配显示问题得到了比较完美的解决。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章