Created
June 8, 2012 19:20
-
-
Save tomohisa/2897676 to your computer and use it in GitHub Desktop.
Add and Remove ChildViewController
This file contains hidden or 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
// add child view | |
UIViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"test"]; | |
[self addChildViewController:controller]; | |
controller.view.frame = CGRectMake(0, 44, 320, 320); | |
[self.view addSubview:controller.view]; | |
[controller didMoveToParentViewController:self]; | |
// remove child view | |
UIViewController *vc = [self.childViewControllers lastObject]; | |
[vc.view removeFromSuperview]; | |
[vc removeFromParentViewController]; |
thanks! you saved my day.
I missed UIViewController *vc = [self.childViewControllers lastObject];
In Swift 3:
Add child view
let controller = storyboard?.instantiateViewController(withIdentifier: "test") as! UIViewController
self.addChildViewController(controller)
controller.view.frame = CGRect(0, 44, 320, 320)
self.view.addSubview(controller.view)
controller.didMove(toParentViewController: self)
Remove child view
let vc = self.childViewControllers.last
vc.removeFromSuperview()
vc.removeFromParentViewController()
I work hard on helping more people when I'm helped :)
vc.view.removeFromSuperview()*
I applied the solution but what to do if i want to add a childViewController that's embedded in a navigation controller (the navigation bar won't appear !!).
and thanks in advance.
Thanks a lot. It works like a charm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also make sure to assign nil to child view controller after removing