Skip to content

Instantly share code, notes, and snippets.

@slaght
Created October 31, 2017 18:26
Show Gist options
  • Save slaght/000a8735220384c6d398cdd46bf3f1f3 to your computer and use it in GitHub Desktop.
Save slaght/000a8735220384c6d398cdd46bf3f1f3 to your computer and use it in GitHub Desktop.
Add a subview to a parent container view and resize the parent to wrap the child
func addSubview(subView:UIView, toView parentView:UIView) {
parentView.addSubview(subView)
var viewBindingsDict = [String: AnyObject]()
viewBindingsDict["subView"] = subView
parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subView]|", options: [], metrics: nil, views: viewBindingsDict))
parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subView]|", options: [], metrics: nil, views: viewBindingsDict))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment