Talk Funnel

Ramin Firoozye's (occasional) Public Whisperings

Semi-Modal (Transparent) Dialogs on the iPhone

Posted by: Ramin on September 29, 2009

Popping up a modal dialog on the iPhone is a fairly straightforward process:

modalDialogViewController *modalController = [[modalDialogViewController alloc]          initWithNibName:@"modalDialogView" bundle:nil];  [self presentModalViewController:modalController animated:YES]; [modalController release];

Dismissing it then is a simple matter of the modalController invoking:

[self dismissModalViewControllerAnimated:YES];

But what if you want to show only half a page’s worth or maybe you need the underlying view to continue being available for user viewing or interaction. Or maybe you want to show a pop-up toolbar where users are asked to choose something before continuing. You might think “Aha! I’ll just have my modal dialog view be half as tall and make the background transparent.”

Go ahead, give it a try. We’ll wait… (* … the girl from Ipanema goes walking … *)

So now you know that the standard modal dialog can only be full-screen and maintains a solid black background. What’s more you can’t interact with what’s behind it because, you know it’s modal — and modal means users shouldn’t be able to do anything else until they’re done with the front-most task (unless you’re the search box in the Contacts app in which case apparently it’s OK to be kindasortamodal).

So what we’re going to do is have a view that can be modal but takes only part of the top view, the space above it remaining visible. What’s more, you can choose to have it so tapping on the background view hides the modal view, or even go full-bore and let the background remain responsive to user input. This technically makes the view semi-modal so let’s ignore the sirens and the UI Police banging on the door and go with that.

The first thing you need is a view that has something interactive on it. The easiest way to build one is in interface builder, so go ahead and make yourself one. For the sake of expedience make it only a fraction of the screen. Here’s an example of a half-height view along with some user controls. The background is set to fully transparent. The view is connected to a UIViewController that reacts to user input:


In this case, the view is the same height as the whole screen because we want the upper portion to be see-through but not react to user input. If we wanted it to be truly interactive, we could make the view height be as tall as the actual content (i.e. half-screen) but that would make it a bit strange for the user because it would be hard to tell apart the actual content from the modal view. But hey, it’s your app. You can do what you want. Another option is to set the background of this view black and partially transparent. That would look cool and show a nice smoky cover while we’re in modal mode… unless you’re mucking with color (like we are in this example) in which case it’s best to leave it fully transparent. Next throw the following code in the parent UIViewController. Load up the UIViewcontroller/UIView you just created and pass the view to this routine instead of calling the standard presentModalViewController method (substitute your application delegate for MyAppDelegate):

// Use this to show the modal view (pops-up from the bottom)
- (void) showModal:(UIView*) modalView {   UIWindow* mainWindow = (((MyAppDelegate*) [UIApplication sharedApplication].delegate).window);   CGPoint middleCenter =;   CGSize offSize = [UIScreen mainScreen].bounds.size;   CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5); = offScreenCenter; // we start off-screen   [mainWindow addSubview:modalView];    // Show it with a transition effect     [UIView beginAnimations:nil context:nil];   [UIView setAnimationDuration:0.7]; // animation duration in seconds = middleCenter;
[UIView commitAnimations]; }

What this does is add your view as a top-level above the main window, effectively rendering it modal. It also uses Core Animation to move the window from offscreen bottom up until it’s fully shown. You should adjust the timing to suit your view’s actual height. I’ve found that the taller the semi-modal view, the more time you should give it to become fully visible. Now let’s go through the hiding action. Note that we use the animation completion handler to do the actual removing of the item from the parent view and cleaning up. We also use the context parameter of the animation call (which was thoughtfully provided for exactly this sort of thing) to keep track of what view to clean up afterward:

// Use this to slide the semi-modal view back down. - (void) hideModal:(UIView*) modalView {   CGSize offSize = [UIScreen mainScreen].bounds.size;   CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);    [UIView beginAnimations:nil context:modalView];   [UIView setAnimationDuration:0.7];   [UIView setAnimationDelegate:self];   [UIView setAnimationDidStopSelector:@selector(hideModalEnded:finished:context:)]; = offScreenCenter;   [UIView commitAnimations]; }  - (void) hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {   UIView* modalView = (UIView *)context; [modalView removeFromSuperview]; } 

[ Update: As noted in the comments, there was an extra release on modalView in hideModalEnded. This code was excerpted from a larger code-base and the release was left in inadvertently. The code listing here has been updated. Thanks for catching it, folks. ]

What I’m not showing you here is the way to trigger the show/hide action. That’s entirely up to you. In standard iPhone modal dialogs this is often a button in the toolbar or navigation bar. In the case of the semi-modal dialog, however, you have even more flexibility. Basically it comes down to the show/hide elements being:

  • Explicit: Provide an Accept or Cancel button on your view.
  • Implicit: You can simulate an action toolbar that shows and hides this way. Put a row of buttons on the view and wire it so tapping each one invokes hideModal before going on to the actual action.
  • Other: Tapping anywhere else on the screen dismisses the dialog. You can do this by placing a full-screen sized view (or custom transparent button) behind your modal dialog and wiring it so it a tap-down action dismisses the dialog . For best results, try making this full-screen view black and semi-transparent (e.g. opacity=0.2). This way the user’s main view darkens so they get a sense your modal dialog is in focus but they still get to see what’s behind.

Here’s a movie of the above semi-modal view in action. It lets the user select a color then confirm or cancel the action. The modal view in this case also has interactive controls on it. As the user changes color the background image changes in real-time so they can visualize what the end-result will be like. Once they’re done they can tap the checkbox or X/cancel buttons to make the modal go away.

The semi-modal dialog is a handy UI interaction component but it’s important to think about how to dismiss the dialog and what to allow in the rest of the visible region on the main window to avoid confusing the user. Also note that there are no restrictions on the shape or size of the overlay view as long as the background color is set to [UIColor clearColor]. You can use the same method for irregularly shaped pop-ups.

Go nuts and have fun.

[Update: Nathan in the comments below has posted some code on github. You may want to check it out. ]