美文网首页
UnityPortraitOnlyViewController

UnityPortraitOnlyViewController

作者: 小西ios | 来源:发表于2022-04-20 18:16 被阅读0次

    Unity写的游戏,转换成了xcode版本后,在presentViewController时有时候会出问题

    Unity原来的控制器是UnityPortraitOnlyViewController 简称A

    要展示的控制器,简称B

    理论上 A执行了presentViewController:animated:completion:方法后,会展示B

    但是很多时候B都是展示在了A的下面,用户看不到B

    由于B是sdk内部的一个控制器,研究半天发现了原因

    B的一个属性modalPresentationStyle设置为了UIModalPresentationOverCurrentContext导致的这个问题

    后台将B的modalPresentationStyle 修改为UIModalPresentationFullScreen,就能正常展示了

    用的方法是给UIViewController加了个Category

    贴一下.m中的代码吧

    #import "UIViewController+ZXC"

    #import <UIKit/UIKit.h>

    #include  <objc/runtime.h>

    @implementation UIViewController (ZXC)

    + (void)load{

        SEL originalSelector =@selector(presentViewController:animated:completion:);

        SEL swizzledSelector =@selector(presentViewController11:animated:completion:);

        Method originalMethod = class_getInstanceMethod([selfclass], originalSelector);

        Method swizzledMethod = class_getInstanceMethod([selfclass], swizzledSelector);

        if(originalMethod && swizzledMethod) {

            method_exchangeImplementations(originalMethod, swizzledMethod);

        }

    }

    -(void)presentViewController11:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void(^)(void))completion{

        if([@"UnityPortraitOnlyViewController" isEqualToString:NSStringFromClass([self class])]) {

            if(viewControllerToPresent.modalPresentationStyle == UIModalPresentationOverCurrentContext) {                       viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;

            }

        }

        [self presentViewController11:viewControllerToPresent animated:flag completion:completion];

    }

    @end

    相关文章

      网友评论

          本文标题:UnityPortraitOnlyViewController

          本文链接:https://www.haomeiwen.com/subject/iqovertx.html