The problem
- A present B, A, B
viewWillAppear
,viewDidAppear
,viewWillDisappear
andviewDidDisappear
What is the order of the calls? - A page
viewWillDisappear
andviewDidDisappear
Under what circumstances will it be called, under what circumstances will it not be called, and why? - After customizing the transition (modalPresentationStyle set to Custom), how to make A page
viewWillDisappear
andviewDidDisappear
Call?
In the project, the previous modal implementation of a transition scene, suddenly need to add a transition animation, thinking that is half an hour to finish the thing, is nothing more than the implementation of two delegates, UIViewControllerTransitioningDelegate and UIViewControllerAnimatedTransitioning, and then a custom animation, a do as a result, almost fall into a pit
Question 1
The scenario looks like this: Present ViewController(page A) with A player on it, and present presentedViewController(page B) in full screen
Before because of modalPresentationStyle setting is UIModalPresentationStyle fullScreen,
So in transition, the lifetime of the two VCS looks something like this
The history logic needs to do something when the presentingViewController(A page) disappears
Question 2
Add a custom transitions, modalPresentationStyle must be set to UIModalPresentationStyle. Custom
ModalPresentationStyle set to UIModalPresentationStyle. The custom, So presentingViewController viewWillDisappear and viewDidDisappear won’t be called
So why didn’t I call it here, I actually know why
Say the first UIModalPresentationStyle. Why call under fullScreen viewWillDisappear and viewDidDisappear
Website to explain
When presenting a view controller using the UIModalPresentationFullScreen style, UIKit normally removes the views of the underlying view controller after the transition animations finish. You can prevent the removal of those views by specifying the UIModalPresentationOverFullScreen style instead. You might use that style when the presented view controller has transparent areas that let underlying content show through.
Main idea:
When using UIModalPresentationFullScreen style presenting a view controller, after completion of the transition animation UIKit usually remove the underlying view controller’s view, You can specify UIModalPresentationOverFullScreen styles to prevent delete these views, when rendered view controller wants to let the underlying content displayed in the transparent area, you can use this style, is to present a page B, The following presentingViewController (A page) is still there
In UIModalPresentationStyle. Why call under fullScreen viewWillDisappear was good and viewDidDisappear explains, because when the ferry, he will remove the viewController below
modalPresentationStyle
forUIModalPresentationOverFullScreen
View structure of
modalPresentationStyle
forUIModalPresentationOverCustom
View structure of
You can see in UIModalPresentationOverCustom mode, presentingViewController (A page), has not been removed, this also explains, Why viewWillDisappear and viewDidDisappear are not called
If you look at the documentation for viewWillDisappear and viewDidDisappear, at what point do those two methods get called
viewwilldisappear
This method is called in response to a view being removed from a view hierarchy. This method is called before the view is actually removed and before any animations are configured
Main idea:
This method is called in response to a view being removed from the view hierarchy. This method is called before the view is actually deleted and before any animations are configured
viewDidDisappear
Notifies the view controller that its view was removed from a view hierarchy
The effect
Notifies the view controller that its view has been removed from the view hierarchy
So at this point, we know that in the modal switch scenario, the presentingViewController(page A) viewwillDisappear and viewDidDisappear calls, It’s about modalPresentationStyle
So what does modalPresentationStyle do as an enumerated property for each of its variables?
ModalPresentationStyle is defined as follows
public enum UIModalPresentationStyle : Int {
case fullScreen = 0
@available(iOS 3.2.*)
case pageSheet = 1
@available(iOS 3.2.*)
case formSheet = 2
@available(iOS 3.2.*)
case currentContext = 3
@available(iOS 7.0.*)
case custom = 4
@available(iOS 8.0.*)
case overFullScreen = 5
@available(iOS 8.0.*)
case overCurrentContext = 6
@available(iOS 8.0.*)
case popover = 7
@available(iOS 7.0.*)
case none = -1
@available(iOS 13.0.*)
case automatic = -2
}
Copy the code
The following notes are abstracted to detail the presentViewController of iOS
UIModalPresentation.fullScreen
Default PRESENTATION style of UIKit. When presented in this mode, the WIDTH and height of the PRESENTATION is the same as that of the screen, and UIKit directly uses rootViewController as the presentation context. After the presentation is complete, UIKit will remove the Presentation context and its children from the UI stack. When observing the hierarchy of VC, only one PRESENTATION VC can be presented under UIWindow.
UIModalPresentation.pageSheet
On conventional devices (such as large-screen phones as the Plus series and iPad series), the Upright Chicago VC features the height of the current screen, the width of the device’s vertical screen, and a transparent background that fills the rest. For compact device (small screens) of horizontal and vertical direction of all equipment, its display effect is the same as the UIModalPresentationFullScreen.
UIModalPresentation.formSheet
Horizontally, presented VC’s width and height are smaller than the screen size, and the rest is filled with transparent backgrounds. For compact device of horizontal and vertical direction of all equipment, its display effect is the same as the UIModalPresentationFullScreen
UIModalPresentation.currentContext
When presented with VC in this manner, the width and height of the PRESENTATION context are determined by the presentation context, And UIKit can find attribute definesPresentationContext to YES VC as the presentation of the context, the specific way of looking for. When the presentation is complete, the Presentation Context and its children will be temporarily removed from the current UI stack.
UIModalPresentation.custom
The custom mode, the need to implement UIViewControllerTransitioningDelegate relevant methods, And will be presented VC transitioningDelegate is set to true UIViewControllerTransitioningDelegate the object of the agreement.
UIModalPresentation.overFullScreen
And the only difference is that UIModalPresentationFullScreen UIWindow besides presented VC, and other normal VC hierarchy. That is, in this mode, UIKit uses rootViewController as the presentation context, but does not remove rootViewController from the current UI stack once the presentation is complete.
UIModalPresentation.overCurrentContext
For the presentation of the context means the same as the UIModalPresentationCurrentContext, the difference is the presentation is completed, will not remove the current context and its sub VC UI stack. However, this approach is applicable only to the transition style is UIModalTransitionStyleCoverVertical UIKit (the default is the transition style). Using this method in other Transition styles will raise an exception.
UIModalPresentation.popover
Mainly used with UIPopoverPresentationController, do a bubble popup window view
UIModalPresentation.automatic
There’s a new style coming out of iOS 13, which is hierarchical
Now that we have an in-depth understanding of modalPresentationStyle, move on to question 3
Question 3
PresentingViewController viewwillDisappear and viewDidDisappear are not called, Is and modalPresentationStyle UIModalPresentation. Related to the custom
So now we need to remove the presentingViewController(page A) from the attempt hierarchy to trigger calls to ViewwillDisappear and viewDidDisappear
Through various research official documentation and StackOverflow
I finally found the answer
Perfect solution to all of the above problems
conclusion
- A present B, A, B
viewWillAppear
,viewDidAppear
,viewWillDisappear
andviewDidDisappear
What is the order of the calls?
🍀🍀🍀 displays PresentedVC 🍀 port PresentingVC viewWillDisappear(_:) PresentedVC viewWillAppear(_:) PresentedVC viewDidAppear(_:) PresentingVC viewDidDisappear(_:)Copy the code
- A page
viewWillDisappear
andviewDidDisappear
Under what circumstances will it be called, under what circumstances will it not be called, and why?
Depending on the modalPresentationStyle type, objects that do not need to be removed from the view hierarchy will not be used, and vice versa
- After customizing the transition (modalPresentationStyle set to Custom), how to make A page
viewWillDisappear
andviewDidDisappear
Call?
Custom UIPresentationController and shouldRemovePresentersView returns true
This article tests the Demo