-
-
Save xgjames/7391bd7469289a413bf1 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]; |
官方文档已经讲得很清楚...
Implementing a Custom Container View Controller
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should call [vc willMoveToParentViewController:nil]; before removeFromSuperview;
https://gist.github.com/tomohisa/2897676#gistcomment-1209661