[iOS] Presentr:正确姿势封装最常见模态视图,使之更易用

Presentr is a simple wrapper for the Custom

[iOS] Presentr:正确姿势封装最常见模态视图,使之更易用

[iOS] Presentr:正确姿势封装最常见模态视图,使之更易用

Presentr is a simple wrapper for the Custom View Controller Presentation API introduced in iOS 8.


It is very common in an app to want to modally present a view on top of the current screen without covering it completely. It can be for presenting an alert, a menu, or any kind of popup with some other functionality.

Before iOS 8 this was done by adding a subiew on top of your content, but that is not the recommended way since a modal should ideally have its own view controller for handling all of the logic. View controller containment was also used, and was a better alternative, but still not ideal for this use case.

iOS 8 fixed all of this by introducing Custom View Controller Presentations, which allowed us to modally present view controllers in new ways. But in order to use this API it is up to us to implement a couple of classes and delegates that could be confusing for some.

Presentris made to simplify this process by hiding all of that and providing a couple of custom presentations and transitions that I think you will find useful. If you want to contribute and add more presentations or transitions please send me a pull request!

What's New


  • Added keyboard observation options for .popup (thanks @aasatt)
  • Swipe to dismiss gesture (thanks @josejuanqm)

See CHANGELOG.md for previous


  1. Fork project
  2. Checkout Developbranch
  3. Create Featurebranch off of the Developbranch
  4. Create awesome feature/enhancement/bug-fix
  5. Optionally create Issueto discuss feature
  6. Submit pull request from your Featurebranch to Presentr’s Developbranch

Supported Swift Versions

Presentr Version Swift Version Min. iOS Version
<= 0.1.8 Swift 2.2 >= iOS 8.0
== 0.2.1 Swift 2.3 >= iOS 8.0
>= 1.0.0 Swift 3.0 >= iOS 9.0



use_frameworks!pod 'Presentr'


Add Presentr to you Cartfile

github "IcaliaLabs/Presentr"

Install using

carthage update --platform ios


  1. Download and drop /Presentrfolder in your project.
  2. You're done!

Main Types

Presentation Type

public enum PresentationType { case alert case popup case topHalf case bottomHalf case fullScreen case custom(width: ModalSize, height: ModalSize, center: ModalCenterPosition)}

Alert & Popup

[iOS] Presentr:正确姿势封装最常见模态视图,使之更易用 [iOS] Presentr:正确姿势封装最常见模态视图,使之更易用

BottomHalf & TopHalf

[iOS] Presentr:正确姿势封装最常见模态视图,使之更易用 [iOS] Presentr:正确姿势封装最常见模态视图,使之更易用

Transition Type

public enum TransitionType{ // System provided case coverVertical case crossDissolve case flipHorizontal // Custom case coverVerticalFromTop case coverHorizontalFromRight case coverHorizontalFromLeft}

Getting Started

Create a Presentr object

It is important to hold on to the Presentr object as a propertyon the presenting/current View Controller since internally it will be used as a delegate for the custom presentation, so you must hold a strong reference to it.

class ViewController: UIViewController{ let presenter: Presentr = { let presenter = Presentr(presentationType: .Alert) presenter.transitionType = .coverHorizontalFromRight // Optional return presenter }()}

The PresentationType (and all other properties) can be changed later on in order to reuse the Presentr object for other presentations.

presenter.presentationType = .popup


Properties are optional, as they all have Default values.

You can choose a TransitionType, which is the animation that will be used to present or dismiss the view controller.

presenter.transitionType = .coverVerticalFromToppresenter.dismissTransitionType = .coverVertical

You can change the background color & opacity for the background view that will be displayed below the presented view controller. Default is black with 0.7 opacity.

presenter.backgroundColor = UIColor.redpresenter.backgroundOpacity = 1.0

You could also turn on the blur effect for the background, and change it's style. Default is false for the blur effect, and .Dark for the style. If you turn on the blur effect the background color and opacity will be ignored.

presenter.blurBackground = truepresenter.blurStyle = UIBlurEffectStyle.light

You can choose to disable rounded corners on the view controller that will be presented. Default is true.

presenter.roundCorners = false

You can choose to disable dismissOnTap that dismisses the presented view controller on tapping the background. Default is true. Or you can disable the animation for the dismissOnTap.

presenter.dismissOnTap = falsepresenter.dismissAnimated = false

Present the view controller.

Instantiate the View Controller you want to present. Remember to setup autolayout on it so it can be displayed well on any size.

let controller = SomeViewController()customPresentViewController(presenter, viewController: controller, animated: true, completion: nil)

This is a helper method provided for you as an extension on UIViewController. It handles setting the Presentr object as the delegate for the presentation & transition.

Creating a custom PresentationType

If you need to present a controller in a way that is not handled by the 4 included presentation types you can create your own. You create a custom PresentationTypeusing the .Customcase on the PresentationTypeenum.

let customType = PresentationType.custom(width: width, height: height, center: center)

It has three associated values for the width, height and center position of the presented controller. For setting them we use two other enums.

// This is used to calculate either a width or height value.public enum ModalSize { case default case half case full case custom(size: Float)}// This is used to calculate the center point position for the modal.public enum ModalCenterPosition { case center case topCenter case bottomCenter case custom(centerPoint: CGPoint) // Custom fixed center point. case customOrigin(origin: CGPoint) // Custom fixed origin point.}

This allows us to use a fixed value when we want

let width = ModalSize.custom(size: 300) // Custom 300pt width

But also let Presentr handle the calculations when we want something more common.

let height = ModalSize.full // Whole screen height

We could also set a fixed position

let position = ModalCenterPosition.custom(centerPoint: CGPoint(x: 150, y: 150)) // Custom center point

Or let presentr calculate the position

let position = ModalCenterPosition.center // Center of the screen

So we can mix and match, and have the benefit of a custom PresentationTypebut still have Presentrcalculating the values we don't want to do ourselves. The following code creates a Presentrobject with a custom PresentationTypewhich shows the alert in a small top banner.

class ViewController: UIViewController{ let customPresenter: Presentr = { let width = ModalSize.full let height = ModalSize.custom(size: 150) let center = ModalCenterPosition.customOrigin(origin: CGPoint(x: 0, y: 0)) let customType = PresentationType.custom(width: width, height: height, center: center) let customPresenter = Presentr(presentationType: customType) customPresenter.transitionType = .coverVerticalFromTop customPresenter.roundCorners = false return customPresenter }()}

[iOS] Presentr:正确姿势封装最常见模态视图,使之更易用

Presentr also comes with a cool AlertViewController baked in if you want something different from Apple's. The API is very similar to Apple's alert controller.

let title = "Are you sure?" let body = "There is no way to go back after you do this!" let controller = Presentr.alertViewController(title: title, body: body) let deleteAction = AlertAction(title: "Sure ��", style: .destructive) { print("Deleted!") } let okAction = AlertAction(title: "NO, sorry ��", style: .cancel){ print("Ok!") } controller.addAction(deleteAction) controller.addAction(okAction) presenter.presentationType = .alert customPresentViewController(presenter, viewController: controller, animated: true, completion: nil)

[iOS] Presentr:正确姿势封装最常见模态视图,使之更易用


  • iOS 9.0+
  • Xcode 8.0+
  • Swift 3.0+


Read the docs. Generated withjazzy.


Daniel Lozano

Main Contributors

Gabriel Peart

Logo design by Eduardo Higareda

Alert design by Noe Araujo


Presentr is released under the MIT license.

See LICENSE for details.