“Embed” UIViewController within another

2019-04-14 16:31发布

I have a UIViewController (DetailViewController) consisting of a navigation bar on the top and a UIView covering the rest of the screen. Is it possible to control the UIView with a UIViewController other than DetailViewController?

2条回答
Ridiculous、
2楼-- · 2019-04-14 17:14

You can also do all this in storyboard. Just drag a container view out into your main view controller and use the embed segue from it to your embedded view controller. It will properly set up all the view controller hierarchy for you.

查看更多
做个烂人
3楼-- · 2019-04-14 17:32

You can do this, but you must not forget to call Apple's required methods for embedding UIViewControllers. Otherwise, your view controller will not get called by the OS to handle certain events.

To add the view controller:

[self addChildViewController:childViewController];                 
[self.view addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];

To remove the view controller:

[childViewController willMoveToParentViewController:nil];  
[childViewController.view removeFromSuperview];            
[childViewController removeFromParentViewController];

Related documentation:

See this question for more information.

查看更多
登录 后发表回答