繁体   English   中英

如何在iPhone iOS7 +上创建透明的模态UIViewController?

[英]How to create transparent, modal UIViewController on iPhones iOS7+?

我有一个相对简单的问题,似乎无法解决。

我位于一个UIViewController(myViewController)中,该UIViewController嵌入在UIViewController层次结构内的某个位置(位于父UITabBarController下方的某个位置)。

myViewController有一个按钮。 当按下该按钮时,我想用黑色,半透明的模态UIViewController(myModalViewController)覆盖整个屏幕(包括来自父UITabBarController的所有粘稠物)以及其他粘稠物。

如果我打电话

[myViewController presentViewController:myModalViewController..]

过渡结束后,myViewController的视觉效果便消失了,从而击败了半透明叠加层的点。

如果我将myModalViewController设置为myViewController的子视图控制器,并将myModalViewController.view添加为myViewController.view的子视图,则具有半透明黑色背景的模态视图控制器将出现在UITabBarController的所有按钮下方。 不好,因为我希望覆盖整个屏幕。

因此,我改为在层次结构中寻找最顶层的视图控制器,然后将myModalViewController添加为其子级:

UIViewController* root = myViewController;
while (root.parentViewController) {
    root = root.parentViewController;
}

[root addChildViewController:myModalViewController];
[root.view addSubview:myModalViewController.view];

如果这是处理这种情况的正确方法? 似乎很糊涂。

另外,如何使myModalViewController表现为模态并吞下所有触摸手势,以使它下面的所有UI都不响应触摸? 现在确实可以,而且我所有的修复尝试都失败了。 而且myModalViewController及其中的所有UIView似乎都没有收到任何触摸通知。

如果您想要透明的ViewController,则必须使用UIModalPresentationCustom东西...

...
[yourModalCtrl setModalPresentationStyle:UIModalPresentationCustom];
[yourModalCtrl setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self setModalPresentationStyle:UIModalPresentationCurrentContext]; //new
//yourModalCtrl.modalPresentationCapturesStatusBarAppearance = YES;
[self presentViewController: yourModalCtrl animated:Yes completion:completion];

[self presentViewController:....]或您的“ Presenting controller”是什么?

http://b2cloud.com.au/how-to-guides/invisible-background-modal-view/

更新

为了与iOS 7兼容,您需要添加新的[self setModalPresentationStyle:UIModalPresentationCurrentContext]; -检查所有代码。 另外,您必须设置animate : No (它与Yes一起工作,但是您将在控制台中看到一条消息:)

在类似的情况下,我所做的是:

我添加了UIImageView作为我的Modal View Controller的背景图像。

然后,我使用了apple提供的UIImage + ImageEffects 类别 ,以创建父视图控制器的模糊图像。

我在父类中实现了一个方法,例如:

// Returns the blurred image
- (UIImage *)getBlurredImage
{
    // You will want to calculate this in code based on the view you will be presenting.
    CGSize size = self.view.frame.size;

    UIGraphicsBeginImageContext(size);

    // view is the view you are grabbing the screen shot of. The view that is to be blurred.
    [self.view drawViewHierarchyInRect:(CGRect)self.view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Gaussian Blur
    image = [image applyDarkEffect];

    // Box Blur
    // image = [image boxblurImageWithBlur:0.2f];

    return image;
}

我将通过此方法生成的UIImage传递给我的模态视图控制器,并将其设置为背景图像(对于我之前添加的图像视图)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM