Created
January 25, 2020 22:59
-
-
Save vinczebalazs/49026cd6fab625fba4de82bec20ab45a to your computer and use it in GitHub Desktop.
UIViewController+Embed
This file contains hidden or 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
| extension UIViewController { | |
| func embed(_ vc: UIViewController, in _containerView: UIView? = nil) { | |
| let containerView: UIView = _containerView ?? view // Use the whole view if no container view is provided. | |
| containerView.addSubview(vc.view) | |
| NSLayoutConstraint.activate([ | |
| vc.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0), | |
| vc.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0), | |
| vc.view.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0), | |
| vc.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0) | |
| ]) | |
| vc.view.translatesAutoresizingMaskIntoConstraints = false | |
| addChild(vc) | |
| vc.didMove(toParent: self) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment