What are the benefits of UIPresentationController over Just using adding subviews and animating the view you want to present in? It appears to be a little be cumbersome with all the protocol it must conform to and many people still prefer presenting their view with simple frame animation. What was Apple logic in introducing the class?
Asked
Active
Viewed 555 times
1 Answers
3
Presentation machinery was a part of UIKit long before UIPresentationController was introduced. UIPresentationController gives more flexibility and allows for creating custom presentations apart from built-in ones (fullscreen, current context, popover, etc). There is a number of reasons for the presentation machinery and UIPresentationController to exist:
- Managing view hierarchies. Whenever you want to present the view you don't always own the presenting view controller. Usually it's discouraged to interfere with the sub-hierarchy of the view controller you don't own but in the case of the presentation you would need to add the presented view controller's view as a child of the presenting view controller's view (or its superview). It becomes more complicated when in-context presentation chains are involved (for example popover -> current context -> share sheet). When presentation machinery is used UIKit takes care of view hierarchy management in a consistent way. It also establishes a container view for the UIPresentationController class to let it focus on managing only the views participating in the presentation. UIPresentationController class may choose to add arbitrary decoration and shadow views to the container view and respond to layout changes in
containerViewWillLayoutSubviews
method. - Managing modality. View controllers are presented and dismissed in a very particular order and presentation controller APIs enforce this. Presentation machinery also makes sure that interaction is disabled for the views that became obscured by the presentation.
- Maintaining view controller hierarchy. Presentation controllers could be viewed as a more specific type of view controller containment, they make sure presented view controllers are still a part of a view controller hierarchy. This is necessary for trait collection propagation, appearance callbacks, sending
viewWillTransitionToSize:withTransitionCoordinator:
, performing view controller transitions (including nested presentations) and a lot more. - Managing first responders. When the presentation happens the current first responder is stashed away and then restored after the dismissal.

egdmitry
- 2,071
- 15
- 18
-
Thanks @egdmitry, I upvoted but your explanation is still as dense as the Apple Doc on UIPresentationController. So I am still quite confused. A more practical comparison of using the class over frame animation could help should you find the time. – irkinosor Jul 13 '16 at 16:24
-
By using the class do you mean subclassing UIPresentationController or using presentation controller machinery in general? Do I understand right the essence of your question is why presentation machinery should be used in general as opposed to direct frame manipulation? – egdmitry Jul 13 '16 at 19:02
-
Yes, egdmitry! I mean using presentation machinery should be used in general as opposed to direct frame manipulation. I find the uipresentation machinery cumbersome when in many cases animating your frame will do the job. So I am trying to understand those cases where you can't do or uipresentation is just much better. – irkinosor Jul 14 '16 at 08:51