kocoai
3/6/2017 - 10:25 PM

[add subview and fill] add subview and fill #tags: uiview

[add subview and fill] add subview and fill #tags: uiview

func addChildViewController(viewController: UIViewController, containerView: UIView) {
	viewController.willMove(toParentViewController: self)
	self.addChildViewController(viewController)
	containerView.addSubviewAndFill(subview: viewController.view)
	viewController.didMove(toParentViewController: self)
}
func addSubviewAndFill(subview: UIView, edgeInsets: UIEdgeInsets = UIEdgeInsets.zero) {
	self.addSubview(subview)
	subview.translatesAutoresizingMaskIntoConstraints = false
	self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(\(edgeInsets.left))-[view]-(\(edgeInsets.right))-|", metrics: nil, views: ["view": subview]))
	self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(\(edgeInsets.top))-[view]-(\(edgeInsets.bottom))-|", metrics: nil, views: ["view": subview]))
}