iOS 全局控制presentview模态推出控制器的modalPresentationStyle的状态值

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 ,如果写的有什么问题,欢迎下方评论

参考链接

iOS13适配
Method Swizzling 理解(hook方法)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值