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 的模态全屏适配显示问题得到了比较完美的解决。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

群鸿

感谢认可,多谢打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值