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).
This is a fairly simple problem to solve. Rather than creating a custom view transition, you just need to set the modalPresentationStyle for the view controller being presented. Also, you should set the background color (and alpha value) for the view controller being presented, in the storyboard / via code.
In the IBAction component of presenting view controller -
You can present a view controller, and still have the original view controller visible underneath, like a form, in iOS 7. To do so, you will need to do two things:
Set the modal presentation style to custom:
Set the transitioning delegate:
In this case, we have set the delegate to self, but it can be another object. The delegate needs to implement the two required methods of the protocol, possible like so:
At this point you may be thinking, where did that
SemiModalAnimatedTransition
class come from. Well, it is a custom implementation adopted from teehan+lax's blog.Here is the class's header:
And the implementation:
Not the most straightforward solution, but avoids hacks and works well. The custom transition is required because by default iOS will remove the first view controller at the end of the transition.
UPDATE:
For iOS 8, once again the landscape has changed. All you need to do is use the new presentation style
.OverCurrentContext
, ie:Try making top view transparent and add a another view below your desired view and make that view's background color black and set alpha 0.5 or whatever opacity level you like.
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 formenuView
to simplify the animation, you could consider skippingmenuViewController
, addingmenuView
to your existing view controllers view hierarchy, and animate it yourself.You should use a property modalPresentationStyle available since iOS 3.2.
For example: