How to create a transparent modal UIViewController?

I would like to create a reusable subclass of UIViewController that can be shown as a modal view controller over any other view controller. One of the first things to do to reuse VC is the UIActionSheet popup. To do this, I create a default view (empty) in my VC to display the action sheet.

However, this looks bad, because when a modal vc pops up, the parent vc is hidden. So it looks like the action sheet is floating over an empty background. It would be better if the action sheet could appear by source (parent) vc.

Is there any way to achieve this? Is it safe to just grab the parent vc view and animate the UIActionSheet?

+7
iphone cocoa-touch
source share
3 answers

After your modal view is animated, the size will be equal in size to its parent view. What you can do is inside your viewDidAppear :, take a picture of the parentController view, then insert a UIImageView containing the parent image at the end of your own subviews list of views:

#pragma mark - #pragma mark Sneaky Background Image - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // grab an image of our parent view UIView *parentView = self.parentViewController.view; // For iOS 5 you need to use presentingViewController: // UIView *parentView = self.presentingViewController.view; UIGraphicsBeginImageContext(parentView.bounds.size); [parentView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *parentViewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // insert an image view with a picture of the parent view at the back of our view subview stack... UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; imageView.image = parentViewImage; [self.view insertSubview:imageView atIndex:0]; [imageView release]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // remove our image view containing a picture of the parent view at the back of our view subview stack... [[self.view.subviews objectAtIndex:0] removeFromSuperview]; } 
+14
source share

You can simply show it on the controller of the parent view by inserting the view into the parent view.

Something like that:

 PseudoModalVC *vc = ...//initialization vc.view.backgroundColor = [UIColor clearColor]; // like in previous comment, although you can do this in Interface Builder vc.view.center = CGPointMake(160, -vc.view.bounds.size.height/2); [parentVC.view addSubView:vc.view]; // animation for pop up from screen bottom [UIView beginAnimation:nil context:nil]; vc.view.center = CGPointMake(160, vc.view.bounds.size.height/2); [UIView commitAnimation]; 
0
source share

Yeah. Add it to the current view of the controller controller (or as a subspecies of the window) and animate it on the screen, as Valerie said.

To remove it using animation, do this (I assume the modal view is 320 x 460 and it will move down from the screen):

 - (void)dismissModal { // animate off-screen [UIView beginAnimations:nil context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; [UIView setAnimationDuration:0.50]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; self.view.frame = CGRectMake( 0, 480, 320, 460 ); [UIView commitAnimations]; } - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { // don't remove until animation is complete. otherwise, the view will simply disappear [self.view removeFromSuperview]; } 
-one
source share

All Articles