如何在iOS中呈现半透明(半切)视图控制器? [英] How to present a semi-transparent (half-cut) viewcontroller in iOS?

查看:107
本文介绍了如何在iOS中呈现半透明(半切)视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来呈现viewcontroller。
我的问题是:动画完成后,透明的主背景会变成不透明的黑色。

I use the following code to present a viewcontroller. My problem is: After the animation completes, the transparent main background becomes opaque black.

如何修复此问题并使其保持为clearColor?

How can I fix this and make it stay as clearColor?

UIViewController *menuViewController=[[UIViewController alloc]init];
   menuViewController.view.backgroundColor=[UIColor clearColor];
   menuViewController.view.tintColor=[UIColor clearColor];
   menuViewController.view.opaque=NO;

UIView *menuView=[[UIView alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-200,320,200)];
   menuView.backgroundColor=[UIColor redColor];

[menuViewController.view addSubview:menuView];

[self presentViewController:menuViewController animated:YES completion:nil];

更新:我正在尝试查看self的内容(演示者视图控制器的视图)。

update: I am trying to see the contents of "self" (the presenter viewcontroller's view).

推荐答案

更新

在大多数情况下,你会想要遵循 Ric的回答,如下。正如他所提到的, menuViewController.modalPresentationStyle = .overCurrentContext 是保持呈现视图控制器可见的最简单的现代方法。

In most cases, you're going to want to follow the guidelines from Ric's answer, below. As he mentions, menuViewController.modalPresentationStyle = .overCurrentContext is the simplest modern way to keep the presenting view controller visible.

我保留了这个答案,因为它为OPs问题提供了最直接的解决方案,他们已经有了一个由当前视图控制器管理的视图,并且只是在寻找一种方法来呈现它,并且因为它解释了实际的原因问题。

I'm preserving this answer because it provided the most direct solution to the OPs problem, where they already had a view managed by the current view controller and were just looking for a way to present it, and because it explains the actual cause problem.

如评论中所述,这不是透明度问题(否则你会发现背景会变成白色)。当 presentViewController:animated:completion:动画完成时,呈现视图控制器实际上已从可视堆栈中删除。您看到的黑色是窗口的背景颜色。

As mentioned in the comments, this isn't a transparency problem (otherwise you would expect the background to become white). When the presentViewController:animated:completion: animation completes, the presenting view controller is actually removed from the visual stack. The black you're seeing is the window's background color.

因为您似乎只是使用 menuViewController 作为主持 menuView 来简化动画,你可以考虑跳过 menuViewController ,添加 menuView 到您现有的视图控制器视图层次结构,并自行设置动画。

Since you appear to just be using menuViewController as a host for menuView to simplify the animation, you could consider skipping menuViewController, adding menuView to your existing view controllers view hierarchy, and animate it yourself.

这篇关于如何在iOS中呈现半透明(半切)视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆