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
#include
@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