DEV Community
Posted on Apr 18, 2019 • Updated on May 14, 2020
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
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
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)
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.
- Presenting View Controllers
- Chapter 19. View Controllers - Presented View Controller
❤️ Support my apps ❤️
- Push Hero - pure Swift native macOS application to test push notifications
- PastePal - Pasteboard, note and shortcut manager
- Quick Check - smart todo manager
- Alias - App and file shortcut manager
- My other apps
❤️❤️😇😍🤘❤️❤️
Top comments (0)
Templates let you quickly answer FAQs or store snippets for re-use.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .
Hide child comments as well
For further actions, you may consider blocking this person and/or reporting abuse
JavaScript Arrays 🔥 Array Awesomeness
Sh Raj - Apr 12
Is it Dry Yet?
Shaun - Apr 12
JavaScript console methods for better debugging 🔮
Construindo um web server em Assembly x86, parte II, história e arquitetura
Leandro Proença - Apr 11
We're a place where coders share, stay up-to-date and grow their careers.
IMAGES
VIDEO
COMMENTS
Presenting a View Controller in a Popover. Popovers require additional configuration before you can present them. After setting the modal presentation style to UIModalPresentationPopover, configure the following popover-related attributes: . Set the preferredContentSize property of your view controller to the desired size.. Set the popover anchor point using the associated ...
11. There are many different ways of presenting a view controller. Common ones are Push, Replace, Presenting Modally, and Presenting as a Popover. Modal Presentation and Popover Presentation give ...
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. ... When a view controller defines a presentation context, it can ...
In this video we will about View Controller transitions and presentation styles in Swift 5 and Xcode 11 for iOS Development. This is a fundamental that every...
As noted above, since iPadOS 13.0 it has been possible to present split view controllers with non-full-screen modal presentation styles. The default presentation of a classic style split view controller adapts to fill the width and has the now familiar card-like sheet appearance where the height falls short of the full height of the screen and the presenting view controller remains partially ...
Presenting the View Controller: Once you have configured the presentation style based on the device type, you can present the view controller using the present(_:animated:completion:) method of ...
In the illustration A represents a popover modal presentation style, B custom, and C fullScreen. It's worth noting that these particular modal presentation styles are not important, rather that I would like to be able to transition between many kind of modal presentation style for a single view controller without first dismissing it.
When presenting a view controller in a popover, this presentation style is supported only if the transition style is UIModalTransitionStyle.coverVertical. Attempting to use a different transition ...
Default Modal Presentation Style Change. The default presentation is now page sheet, not full screen. ... Adaptivity default modal presentation of Split View Controller on 11" iPad running iOS 13. Presenting a split screen view controller is not something most apps would do, but it is one of the views in my Adaptivity app. ...
UIKit separates the content of your view controllers from the way that content is presented and displayed onscreen. Presented view controllers are managed by an underlying presentation controller object, which manages the visual style used to display the view controller's view. A presentation controller may do the following:
This presentation controller has a simple method which sets the presentation style for the view controller we want to present, it sets itself as the transitioning delegate and then performs the ...
Bear that in mind if you are using not only the automatic style but also presentation styles like .popover etc. This property is false by default. From the official docs: If true, UIKit ignores events outside the view controller's bounds and prevents the interactive dismissal of the view controller while it is onscreen.
This method is your opportunity to replace the current view controller with one that is better suited for the new presentation style. For example, you might use this method to insert a navigation controller into your view hierarchy to facilitate pushing new view controllers more easily in the compact environment.
Separation of Concerns: MVC enforces a clear separation of data (Model), presentation (View), and logic (Controller), making the codebase more organized and maintainable. 2. Code Reusability: With ...