iOS 原生分享模块遮挡了视图的解决办法

UIActivityViewController原生分享模块遮挡了视图的解决办法。
如图,视图加载到KeyWindow,UIActivityViewController类却在夹到中间了。


image.png

方法一:很明显将遮挡的视图加到当前控制器,这样子UIActivityViewController就不会被遮挡了,让我们先试一下。

image.png

这时候发觉,UIActivityViewController是不会被遮挡了,但tabBar区域会遮挡弹框出来的部分图层,即使你将tabBar隐藏了,接着修改当前所有控制器的涂层高度,让它等同于屏幕宽~~然而,这真的是太麻烦了。

随手一试,报错: should have parent view controller。看来系统方法真的不能按着自己的想法来了。

UIWindow * window = [[UIApplication sharedApplication] keyWindow];
        //[window.rootViewController presentViewController:activityVC animated:YES completion:nil];
        [window.rootViewController addChildViewController:activityVC];
        
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        UIView *activityView = activityVC.view;
        [keyWindow addSubview:activityView];
weGood[11959:157654] *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller: should have parent view controller:(null) but actual parent is:'
*** First throw call stack:

最终解决办法

if(shareImgs.count>1){//调用原生分享
        UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:shareImgs applicationActivities:nil];
        
        //去除一些不需要的图标选项
        activityVC.excludedActivityTypes = @[UIActivityTypeAirDrop, UIActivityTypePostToWeibo, UIActivityTypePostToTencentWeibo];
        
        //成功失败的回调block
        __weak __typeof(self)weakSelf = self;
        UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
            
            [weakSelf.vc.view addSubview:weakSelf];
            UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
            [keyWindow addSubview:weakSelf];
            
            if (completed){
                [MBProgressHUD showSuccess:@"谢谢分享!"];
            }else{
                NSLog(@"************Share fail with error %@*********",activityError);
            }
        };
        activityVC.completionWithItemsHandler = myBlock;
        
        
        UIWindow * window = [[UIApplication sharedApplication] keyWindow];
        [window.rootViewController presentViewController:activityVC animated:YES completion:nil];
        
        [self removeFromSuperview];
        [self.vc.view addSubview:self];
    }
image.png

最终图层


image.png

--end.

你可能感兴趣的:(iOS 原生分享模块遮挡了视图的解决办法)