I have the following 2 functions that add and remove child view controllers triggered from a container view controller:
@discardableResult func addChildViewController(withChildViewController childViewController: UIViewController) -> UIViewController {
// Add Child View Controller
addChildViewController(childViewController)
childViewController.beginAppearanceTransition(true, animated: true)
// Add Child View as Subview
view.addSubview(childViewController.view)
// Configure Child View
childViewController.view.frame = view.bounds
childViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Notify Child View Controller
childViewController.didMove(toParentViewController: self)
return childViewController
}
@discardableResult func removeChildViewController(withChildViewController childViewController: UIViewController) -> UIViewController {
// Notify Child View Controller
childViewController.willMove(toParentViewController: nil)
childViewController.beginAppearanceTransition(false, animated: true)
// Remove Child View From Superview
childViewController.view.removeFromSuperview()
// Notify Child View Controller
childViewController.removeFromParentViewController()
return childViewController
}
The functions above are extensions to UIViewController, so all I'm doing is self.addChildViewController() and self.removeChildViewController() on the parent view controller.
How do I animate the view being removed on its way out and the view being added on its way in?
Answer of Sagar is not complete
addSubview
method is missing thereHere is full answer
Hope it is helpful to someone
Animating between different child view controllers:-
In above,
To animate child view, you can use different type of animation style by replacing transitionFlipFromLeft to available UIViewAnimationOptions according requirement.