DEV Community

Khoa Pham
Khoa Pham

Posted on • Edited on

Understanding presentingViewController in iOS

present(_:animated:completion:)

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621380-present

The object on which you call this method may not always be the one that handles the presentation. Each presentation style has different rules governing its behavior. For example, a full-screen presentation must be made by a view controller that itself covers the entire screen. If the current view controller is unable to fulfill a request, it forwards the request up the view controller hierarchy to its nearest parent, which can then handle or forward the request.

Suppose we are trying to present viewController2 onto viewController1

vc1.present(v2, animated: true, completion: nil)

After this vc1.presentedViewController == vc2. You may also think that vc2.presentingViewController == vc1 but this is not always the case.
When a presenting view controller happens, there may be 3 players in the game

Presented view controller

This is vc2

Original presenter

This is vc1, and sometime called source view controller. At this stage, vc1.presentedViewController == vc2

Presenting view controller

This is vc2.presentingViewController, but it is not always vc1. This is the view controller whose view is replaced or covered by vc2.view. It is by default the view controller whose view occupies the entire screen, it can be either root view controller or an already presented view controller. It might not be the same as vc1.

For example, the presenting view controller is the root view controller, we have rootViewController.presentedViewController == vc2. So in this example, vc2 can be a presentedViewController of both original presenter (vc1) and presenting view controller (root view controller)

If vc1 occupies the whole screen, original presenter and presenting view controller are the same (vc1)

In another world, vc2.presentingViewController will only point to the real presenting view controller.

dismiss(animated:completion:)

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss

We can send dismiss(animated:completion:) to any of those 3 objects, the system will forward that message to the presenting view controller.

Continue with the example of the rootViewController as the real presenting view controller, if you say

vc2.dismiss(animated: true, completion: nil)

then root view controller with be the one that receives this message.

Chain of presented view controller

A view controller can have at most 1 presentedViewController at a time. However, a presented view controller (vc2) can itself presents another view controller (vc3)

vc2.present(vc3, animated: true, completion: nil)

Presentation context

So what does it mean by "occupy the whole screen". Read Presentation Contexts Provide the Area Covered by the Presented View Controller

The area of the screen used to define the presentation area is determined by the presentation context. By default, the presentation context is provided by the root view controller, whose frame is used to define the frame of the presentation context. However, the presenting view controller, or any other ancestor in the view controller hierarchy, can choose to provide the presentation context instead. In that case, when another view controller provides the presentation context, its frame is used instead to determine the frame of the presented view. This flexibility allows you to limit the modal presentation to a smaller portion of the screen, leaving other content visible.

When a view controller is presented, iOS searches for a presentation context. It starts at the presenting view controller by reading its definesPresentationContext property. If the value of this property is YES, then the presenting view controller defines the presentation context. Otherwise, it continues up through the view controller hierarchy until a view controller returns YES or until it reaches the window’s root view controller.

When a view controller defines a presentation context, it can also choose to define the presentation style. Normally, the presented view controller determines how it presented using its modalTransitionStyle property. A view controller that sets definesPresentationContext to YES can also set providesPresentationContextTransitionStyle to YES. If providesPresentationContextTransitionStyle is set to YES, iOS uses the presentation context’s modalPresentationStyle to determine how the new view controller is presented.

definesPresentationContext

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621456-definespresentationcontext

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.

Read more


❤️ Support my apps ❤️

❤️❤️😇😍🤘❤️❤️

Top comments (0)