Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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/

Jesse Squires

January 28, 2016
Tweet

More Decks by Jesse Squires

Other Decks in Programming

Transcript

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

    into the transitioning API. Slightly coupled in this way, but otherwise nicely decoupled.
  2. UIPresentationController The APIs are very subtle and usually implicit. You

    often use them without knowing it. Typically overlooked. Previously, relevant on iPad only.
  3. 3 PHASES 1. Presentation (moving onscreen)* 2. Management (size class

    changes, device rotation) 3. Dismissal (moving offscreen)* *Transitioning
  4. UIPresentationController Subtle and Implicit UIModalPresentationStyle // just set an enum!

    self.modalPresentationStyle = .FullScreen self.modalPresentationStyle = .Popover
  5. UIPopoverPresentationController The only custom presentation controller publicly available in UIKit

    // Current UIViewController APIs self.presentationController self.popoverPresentationController
  6. PRIVATE API // Modal styles: .FullScreen, .FormSheet, .PageSheet, etc. _UIFullscreenPresentationController

    // Alerts _UIAlertControllerAlertPresentationController // ActionSheet on iPhone _UIAlertControllerActionSheetCompactPresentationController // ActionSheet on iPad (popover) _UIAlertControllerActionSheetRegularPresentationController
  7. API SUMMARY ▸ 4 methods to display a view controller

    ▸ Set styles via an enum value ▸ Use popoverPresentationController directly ▸ Provide a custom Presentation Controller
  8. 2. VEND CONTROLLER UIViewControllerTransitioningDelegate Presenting view controller conforms to delegate

    Set this delegate on the Presented view controller let vc = MyViewController() vc.transitioningDelegate = self
  9. enum PresentationType { case Modal(NavigationStyle, UIModalPresentationStyle, UIModalTransitionStyle) case Popover(PopoverConfig) case

    Push case Show case ShowDetail(NavigationStyle) case Custom(UIViewControllerTransitioningDelegate) }
  10. struct PopoverConfig { enum Source { case BarButtonItem(UIBarButtonItem) case View(UIView)

    } let source: Source let arrowDirection: UIPopoverArrowDirection let delegate: UIPopoverPresentationControllerDelegate? }
  11. extension UIViewController { // maps PresentationType to UIKit properties and

    methods func presentViewController( controller: UIViewController, type: PresentationType, animated: Bool = true) }
  12. 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))
  13. Swifty API ▸ More expressive ▸ More concise ▸ Single

    "entry point" to API ▸ More structured (less likely to misuse)
  14. Additional resources: ▸ WWDC 2014 "A Look Inside Presentation Controllers"

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