Skip to content

Instantly share code, notes, and snippets.

@vinczebalazs
Created January 25, 2020 22:59
Show Gist options
  • Select an option

  • Save vinczebalazs/49026cd6fab625fba4de82bec20ab45a to your computer and use it in GitHub Desktop.

Select an option

Save vinczebalazs/49026cd6fab625fba4de82bec20ab45a to your computer and use it in GitHub Desktop.
UIViewController+Embed
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