IcaliaLabs / Presentr
- воскресенье, 3 июля 2016 г. в 03:13:02
Swift
Wrapper for custom ViewController presentations in iOS 8+
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.
Presentr is 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!
use_frameworks!
pod 'Presentr'
public enum PresentationType {
case Alert
case Popup
case TopHalf
case BottomHalf
}
public enum TransitionType{
// System provided
case CoverVertical
case CrossDissolve
case FlipHorizontal
// Custom
case CoverVerticalFromTop
case CoverHorizontalFromRight
case CoverHorizontalFromLeft
}
It is important to hold on to the Presentr object as an instance variable(property) on the presenting/current View Controller since internally it will be used as a delegate for the custom presentation.
let presenter = Presentr(presentationType: .Alert)
presenter.transitionType = .CoverHorizontalFromRight // Optional
Both types can be changed later on in order to reuse the Presentr object for other presentations.
presenter.presentationType = .Popup
presenter.transitionType = .CoverVerticalFromTop
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.
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)
Read the docs. Generated with jazzy.
Daniel Lozano
iOS Developer @ Icalia Labs
Logo design by Eduardo Higareda
Alert design by Noe Araujo
Presentr is released under the MIT license.
See LICENSE for details.