Skip to content

Instantly share code, notes, and snippets.

@xtrinch
Created August 3, 2016 12:56
Show Gist options
  • Save xtrinch/a920330f0b15b6079fd5716b82fd5514 to your computer and use it in GitHub Desktop.
Save xtrinch/a920330f0b15b6079fd5716b82fd5514 to your computer and use it in GitHub Desktop.
Add and remove childviewcontroller in swift
// in parent view controller
@IBAction func showChildViewController(sender: AnyObject) {
let testController = self.storyboard!.instantiateViewControllerWithIdentifier("Test") as! Test
self.addChildViewController(testController)
self.view.addSubview(testController.view)
testController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
testController.didMoveToParentViewController(self)
}
// in child view controller
@IBAction func dismissTapped(sender: AnyObject) {
self.view.removeFromSuperview()
self.removeFromParentViewController()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment