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

来源:互联网 发布:sql server 月份差 编辑:程序博客网 时间:2024/05/07 18:35

问题在做分享到第三方的时候,要弹出一个分享框,底部的背景会变暗。在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