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

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

问题描述

我使用以下代码来呈现一个视图控制器.我的问题是:动画完成后,透明的主背景变成不透明的黑色.

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.

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

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天全站免登陆