iOS 7.0 presentViewController 背景变黑的解决办法

问题在做分享到第三方的时候,要弹出一个分享框,底部的背景会变暗。在iOS8.0以上可以直接设置,但在iOS8.0以下会出现背景变黑的情况。


原因分析:Why Does presentModalViewController:animated: Turn The Background Black?

                    Display clearColor UIViewController over UIViewController

找到原因:    (界面同一时刻只能展示一个View Controller)

NavigationController and the View Controllers are designed in such a way that only one view controller may show at a time. When a new view controller is pushed/presented the previous view controller will be hidden by the system. So when you reduce the modal view's alpha you will possibly see the window's backgroundColor (the black color you see now).

If you want a translucent view to slide-in over the main view, you can add the view as the subView of main view and animate it using UIView Animations.

解决办法: iOS7.0时在弹出分享的界面的时候截取当前屏幕的图片,然后在弹出分享的界面之后将图片的ImageView添加在分享控制器的视图下(index = 0)。

//展示分享的控制器
-(void)presentShareViewController:(UIViewController*)controller{

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        controller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self presentViewController:controller animated:YES completion:nil];
    }else if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){
        UIImageView *bottomImageView = [self getBottomImageView];
        controller.modalPresentationStyle = UIModalPresentationCurrentContext;
        [self presentViewController:controller animated:YES completion:^{
            [controller.view insertSubview:bottomImageView atIndex:0];
        }];
    }
}

//获取添加在controller.view上的ImageView
-(UIImageView*)getBottomImageView{

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [[[UIApplication sharedApplication] keyWindow].layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView *bottomImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bottomImageView.userInteractionEnabled = YES;
    bottomImageView.alpha = 0.8;
    [bottomImageView setImage:img];
    return bottomImageView;
}

用法在展示分享控制器时,只要调用presentShareViewController即可。






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值