Swifty View Controller Presenters

Swifty View Controller Presenters

One major shortcoming of UIKit is that view controllers have too many responsibilities. This talk focuses on one — presenting and dismissing view controllers — and how we can re-examine and redefine the presentation controller API in iOS with a more Swifty API that reduces boilerplate and increases expressivity.

Video:
https://realm.io/news/slug-jesse-squires-swifty-view-controller-presenters/

GitHub project:
https://github.com/jessesquires/PresenterKit

Event:
https://www.meetup.com/swift-language/events/227833264/

Ba6b43b7b6198e2c20cbd348431ca6f4?s=128

Jesse Squires

January 28, 2016
Tweet

Transcript

  1. 7.
  2. 8.
  3. 9.

    TRANSITION VS. PRESENTATION Concepts are very interrelated. Presentation API hooks

    into the transitioning API. Slightly coupled in this way, but otherwise nicely decoupled.
  4. 12.

    UIPresentationController The APIs are very subtle and usually implicit. You

    often use them without knowing it. Typically overlooked. Previously, relevant on iPad only.
  5. 15.

    3 PHASES 1. Presentation (moving onscreen)* 2. Management (size class

    changes, device rotation) 3. Dismissal (moving offscreen)* *Transitioning
  6. 16.
  7. 17.
  8. 18.
  9. 19.
  10. 20.

    UIPresentationController Subtle and Implicit UIModalPresentationStyle // just set an enum!

    self.modalPresentationStyle = .FullScreen self.modalPresentationStyle = .Popover
  11. 21.
  12. 26.

    UIPopoverPresentationController The only custom presentation controller publicly available in UIKit

    // Current UIViewController APIs self.presentationController self.popoverPresentationController
  13. 27.

    PRIVATE API // Modal styles: .FullScreen, .FormSheet, .PageSheet, etc. _UIFullscreenPresentationController

    // Alerts _UIAlertControllerAlertPresentationController // ActionSheet on iPhone _UIAlertControllerActionSheetCompactPresentationController // ActionSheet on iPad (popover) _UIAlertControllerActionSheetRegularPresentationController
  14. 28.

    API SUMMARY ▸ 4 methods to display a view controller

    ▸ Set styles via an enum value ▸ Use popoverPresentationController directly ▸ Provide a custom Presentation Controller
  15. 36.

    2. VEND CONTROLLER UIViewControllerTransitioningDelegate Presenting view controller conforms to delegate

    Set this delegate on the Presented view controller let vc = MyViewController() vc.transitioningDelegate = self
  16. 38.
  17. 41.

    enum PresentationType { case Modal(NavigationStyle, UIModalPresentationStyle, UIModalTransitionStyle) case Popover(PopoverConfig) case

    Push case Show case ShowDetail(NavigationStyle) case Custom(UIViewControllerTransitioningDelegate) }
  18. 43.

    struct PopoverConfig { enum Source { case BarButtonItem(UIBarButtonItem) case View(UIView)

    } let source: Source let arrowDirection: UIPopoverArrowDirection let delegate: UIPopoverPresentationControllerDelegate? }
  19. 44.

    extension UIViewController { // maps PresentationType to UIKit properties and

    methods func presentViewController( controller: UIViewController, type: PresentationType, animated: Bool = true) }
  20. 45.

    USAGE presentViewController(vc, type: .Push) let config = PopoverConfig(source: .View(v)) presentViewController(vc,

    type: .Popover(config)) let type = .Modal(.WithNavigation, .FullScreen, .CoverVertical) presentViewController(vc, type: type) presentViewController(vc, type: .Custom(self))
  21. 46.

    Swifty API ▸ More expressive ▸ More concise ▸ Single

    "entry point" to API ▸ More structured (less likely to misuse)
  22. 47.

    Additional resources: ▸ WWDC 2014 "A Look Inside Presentation Controllers"

    ▸ pspdfkit.com/blog/2015/presentation-controllers/ ▸ petersteinberger.com/blog/2015/uipresentationcontroller-popover- detection/