The Presenter: going modal (popover or fullscreen) on iOS7 and iOS8

When we develop universal apps, we sometimes need to present  modal view controllers (“dialogs”) to the user.

On the iPad the popover is very useful to this aim because the user does not loose the context while reading the content of the dialog or while filling some inputs inside it.

On the other side, the iPhone usually has not enough space to show a popover inside the parent (or “presenting”) view controller (however this is possible now in iOS8). So on the iPhone we may want to present the new view controller in a modal full screen mode.

iOS8 helps us to handle elegantly this scenario but (almost) all of us needs to maintain compatibility with iOS7.

In this post we will develop the class “Presenter” that we will use in our universal iOS7 and iOS8 apps to transparently handle this kind of presentation.

Presenter API

The presenter class is a “singleton” and has two methods, one presents a view controller:

and another dismisses it:

A typical use of Presenter  is to instantiate a view controller from the storyboard and then present it:

The presented view controller will then have an action to dismiss itself:

Result

On the iPhone the tap on a button presents a new full screen dialog (result is the same for iOS7 and iOS8):

Presenter_iPhone

On the iPad, with the same code, a popover is used:

iOS8:

presenter_iPad8

iOS7

presenter_iPad7

(note the little difference of the arrow position)

iOS7 implementation

On iOS7 we can present a view controller using a popover only if we are on the iPad (the UIPopoverController is not available on the iPhone).

So before presenting the popover we check the device type (or the “idiom”)

If we are on the iPad we use a popover, otherwise the view controller is presented modally:

We maintain the popover controller in one of ours properties:

because we need it in the dismiss (when we used a popover in the presentation):

iOS8 implementation

iOS8 is “adaptive”. Among other things, this means that the APIs behave differently (and transparently) depending on the device (or screen size) they run on.

This means that with can write a code and it will adjust automatically to different scenarios. The new UIPopoverPresentationController class handles the presentation of content in a popover. If the screen size require a modal full screen, it is applied (and the popover ignored):

Note also that in iOS8 we don’t use the popoverController class but the combination of the new popoverPresentationController and the usual presentViewController (popover is treated as a normal presentation).

And the dismiss is very simple:

Conclusion

In this post we built a Presenter class that transparently presents a new view controller as a popover (on the iPad) or as a modal full screen (on the iPhone) and works on iOS7 and iOS8.

This is also a good way to study the differences between the way iOS7 and iOS8 handles the presentation and dismissal of a popover.

Note that we could have used only the iOS7 code and the result would be (almost) identical. But implementing both modes (7 and 8) our code is already prepared (and tested) for a future iOS8-only scenario.

The XCode (6.1.1) project of the post can be found on GitHub.

 

Valerio Ferrucci

Valerio Ferrucci (valfer) develops software on Apple Macintosh since the 1990s until today for MacOS, OSX and, since some years, iOS. He is also a Web (PHP/MySQL/JS/CSS) and Android Developer.

More Posts - Website

Follow Me:
TwitterFacebookLinkedIn

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

One thought on “The Presenter: going modal (popover or fullscreen) on iOS7 and iOS8”

Leave a Reply

Your email address will not be published. Required fields are marked *