Home > Articles > Home & Office Computing > Entertainment/Gaming/Gadgets

  • Print
  • + Share This
This chapter is from the book

Recipe: Presenting a Custom Modal Information View

Modal view controllers slide onscreen without being part of your standard view controller stack. Modal views are useful for picking data, updating settings, performing an orthogonal function, or presenting information, tasks that might not match well to your normal hierarchy. Any view controller, including navigation controllers, can present a modal controller as demonstrated in the Chapter 4 walkthroughs. This recipe introduces modal controllers more from code point of view.

Presenting a modal controller branches off from your primary navigation path, introducing a new interface that takes charge until your user explicitly dismisses it. You present a modal controller like this.

[self presentModalViewController:someControllerInstance animated:YES];

The controller that is presented can be any kind of view controller subclass, as well. In the case of a navigation controller, the modal presentation can have its own navigation hierarchy built as a chain of interactions.

Always provide a Done button to allow users to dismiss the controller. The easiest way to accomplish this is to present a navigation controller, adding a bar button to its navigation items. Figure 5-4 shows a modal presentation built around a UIViewController instance using a page curl presentation. You can see the built-in Done button at the top-right of the presentation.

Figure 4 (Click to Enlarge) This modal view is built using UIViewController with a UINavigationBar.

In iOS 5.x, modal presentations can use four transition styles. They are:

  • Slide transitions slide a new view over the old.
  • Fade dissolves the new view into visibility.
  • Flip turns a view over to the "back" of the presentation.
  • Curl, which is shown in Figure 5-4, makes the primary view curl up out of the way to reveal the new view beneath it.

In addition to these transition styles, the iPad offers three presentation styles. These include:

  • A Full Screen presentation is the default on the iPhone, where the new modal view completely covers both the screen and any existing content. This is the only presentation style that is legal for curls – any other presentation style raises a run-time exception, crashing the application.
  • In the Page Sheet, coverage defaults to a portrait aspect ratio, so the modal view controller completely covers the screen in portrait mode and partially covers the screen in landscape mode, as if a portrait-aligned piece of paper were added to the display.
  • The Form Sheet display covers a small center portion of the screen, allowing you to shift focus to the modal element while retaining the maximum visibility of the primary application view.

Your modal view controllers must auto rotate. This skeleton demonstrates the simplest possible modal controller you should use. Notice the Interface-Builder-accessible done: method.

@interface ModalViewController : UIViewController
- (IBAction)done:(id)sender;

@implementation ModalViewController
- (IBAction)done:(id)sender 
    [self dismissModalViewControllerAnimated:YES];

- (BOOL) shouldAutorotateToInterfaceOrientation:
    return YES;

Storyboards simplify the creation of modal controller elements. Drag in a navigation controller instance, along with its paired view controller, adding a Done button to the provided navigation bar. Set the view controller's class to your custom modal type and connect the Done button to the done: method. Make sure you name your navigation controller in the attributes inspector, so you can use that identifier to load it.

You can either add the modal components to your primary storyboard or create them in a separate file. Recipe 5-4 loads a custom file (Modal~DeviceType.storyboard) but you can just as easily add the elements in your MainStoryboard_DeviceType file.

Recipe 5-4 offers the key pieces for creating modal elements. The presentation is performed in the application's main view controller hierarchy. Here, users select the transition and presentation styles from segmented controls, but these are normally chosen in advance by the developer and set in code or in IB. This recipe offers a toolbox that you can test out on each platform, using each orientation, to explore how each option looks.

Recipe 5-4  Presenting and Dismissing a Modal Controller

// Presenting the controller
- (void) action: (id) sender
    // Load info controller from storyboard
    UIStoryboard *sb = [UIStoryboard 
        storyboardWithName: (IS_IPAD ? @"Modal~iPad" : @"Modal~iPhone") 
        bundle:[NSBundle mainBundle]];
    UINavigationController *navController = 
        [sb instantiateViewControllerWithIdentfier:
    // Select the transition style
    int styleSegment = 
        [(UISegmentedControl *)self.navigationItem.titleView 
    int transitionStyles[4] = {
    navController.modalTransitionStyle = transitionStyles[styleSegment];
    // Select the presentation style for iPad only
    if (IS_IPAD)
        int presentationSegment = 
            [(UISegmentedControl *)[[self.view subviews] 
                lastObject] selectedSegmentIndex];
        int presentationStyles[3] = {
        if (navController.modalTransitionStyle == 
            // Partial curl with any non-full screen presentation 
            // raises an exception
            navController.modalPresentationStyle = 
            [(UISegmentedControl *)[[self.view subviews] 
                lastObject] setSelectedSegmentIndex:0];
            navController.modalPresentationStyle = 

    [self.navigationController presentModalViewController:
         navController animated:YES];

- (void) loadView
    [super loadView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = 
        BARBUTTON(@"Action", @selector(action:));
    UISegmentedControl *segmentedControl = 
        [[UISegmentedControl alloc] initWithItems:
            [@"Slide Fade Flip Curl" componentsSeparatedByString:@" "]];
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    self.navigationItem.titleView = segmentedControl;
    if (IS_IPAD)
        NSArray *presentationChoices = 
           [NSArray arrayWithObjects:
               @"Full Screen", @"Page Sheet", @"Form Sheet", nil];
        UISegmentedControl *iPadStyleControl = 
            [[UISegmentedControl alloc] init
        iPadStyleControl.segmentedControlStyle = 
        iPadStyleControl.autoresizingMask = 
        iPadStyleControl.center = 
            CGPointMake(CGRectGetMidX(self.view.bounds), 22.0f);
        [self.view addSubview:iPadStyleControl];
  • + Share This
  • 🔖 Save To Your Account