天天看点

ios 设置控制器背景半透明,如何在iOS中呈现半透明(半切)视图控制器?

ios 设置控制器背景半透明,如何在iOS中呈现半透明(半切)视图控制器?

I use the following code to present a viewcontroller.

My problem is: After the animation completes, the transparent main background becomes opaque black.

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];

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

解决方案

Update

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.

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.

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.

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.