Skip to content

Instantly share code, notes, and snippets.

@ibrahimkteish
Created November 3, 2016 23:43
Show Gist options
  • Save ibrahimkteish/ff4b186617a541fe4bb453ceb1ab3d04 to your computer and use it in GitHub Desktop.
Save ibrahimkteish/ff4b186617a541fe4bb453ceb1ab3d04 to your computer and use it in GitHub Desktop.
Add Remove UIViewController Extension
//MARK: UIViewController
extension UIViewController {
func IKAddChildViewController(child:UIViewController) {
child.willMove(toParentViewController: self)
addChildViewController(child)
child.beginAppearanceTransition(true, animated: true)
view.addSubview(child.view)
child.endAppearanceTransition()
child.view.translatesAutoresizingMaskIntoConstraints = false
let viewsDictionary : [String : UIView] = ["child":child.view]
let child_constraint_H:Array = NSLayoutConstraint.constraints(withVisualFormat: "H:|[child]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let child_constraint_V:Array = NSLayoutConstraint.constraints(withVisualFormat: "V:|[child]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
view.addConstraints(child_constraint_H)
view.addConstraints(child_constraint_V)
child.didMove(toParentViewController: self)
}
func IKRemoveChildViewController(child:UIViewController) {
child.willMove(toParentViewController: nil)
child.beginAppearanceTransition(false, animated: true)
child.view.removeFromSuperview()
child.endAppearanceTransition()
child.removeFromParentViewController()
child.didMove(toParentViewController: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment