Last active
September 5, 2022 07:27
-
-
Save michaelevensen/3bbd36e417e463c01368ff5669a428c0 to your computer and use it in GitHub Desktop.
Embed a UIViewController in Container View programatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instantiate | |
self.mapViewController = self.storyboard?.instantiateViewController(withIdentifier: "MapViewController") as? MapViewController | |
if let mapViewController = self.mapViewController { | |
// Add to Container View | |
self.mapViewContainerView.addSubview(mapViewController.view) | |
self.addChildViewController(mapViewController) | |
mapViewController.didMove(toParentViewController: self) | |
} | |
} |
👍
willMove
documentation:
When your custom container calls the addChild(_:) method, it automatically calls the willMove(toParent:) method of the view controller to be added as a child before adding it.
Looks like there is no need to call the willMove
method since just after that you call the addChildViewController
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
He is correct. I have tested this and in the above example the
willMode
is called 2x