Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are definesPresentationContext and providesPresentationContextTransitionStyle

Tags:

ios

ViewController has definesPresentationContext and providesPresentationContextTransitionStyle, but have no idea about how they work. I watched the WWDC2011, but I'm still confused about these two things.

Could anyone explain this, preferably with a simple demo?

like image 500
NickYu Avatar asked Aug 22 '14 11:08

NickYu


1 Answers

Both properties are used for view controller containment, and provide child view controllers the option to override the presentation context or presentation style of the window's root view controller. For reference, here is the relevant WWDC presentation that provides an explanation of both:

https://developer.apple.com/videos/play/wwdc2011-102/

  • definesPresentationContext is used to decide which view controller will determine the size of the presented view controller's view:

When a view controller is presented, iOS starts with the presenting view controller and asks it if it wants to provide the presentation context. If the presenting view controller does not provide a context, then iOS asks the presenting view controller's parent view controller. iOS searches up through the view controller hierarchy until a view controller provides a presentation context. If no view controller offers to provide a context, the window's root view controller provides the presentation context.

If a view controller returns YES, then it provides a presentation context. The portion of the window covered by the view controller's view determines the size of the presented view controller's view. The default value for this property is NO.

  • providesPresentationContextTransitionStyle is used to decide which modal presentation style should be used when presenting a child view controller:

When a view controller’s definesPresentationContext property is YES, it can replace the transition style of the presented view controller with its own. When the value of this property to YES, the current view controller’s transition style is used instead of the style associated with the presented view controller. When the value of this property is NO, UIKit uses the transition style of the presented view controller. The default value of this property is NO.

For complex child view controllers such as UISearchController, it's a good idea to have these set to true, the default value is false.

like image 105
Jake Spracher Avatar answered Sep 18 '22 20:09

Jake Spracher