40

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?

2 Answers 2

56

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.

0
0

definesPresentationContext is related to child view controllers which in storyboards are represented by Container View. So, this option influence a presentation of a new UIViewController from a container view.

providesPresentationContextTransitionStyle is related also to child view controllers and will take effect only when definesPresentationContext is true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.