Created
November 3, 2016 23:43
-
-
Save ibrahimkteish/ff4b186617a541fe4bb453ceb1ab3d04 to your computer and use it in GitHub Desktop.
Add Remove UIViewController Extension
This file contains 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
//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