iOS - Programmatically Set A UIContainerView's Embedded UIViewController

£可爱£侵袭症+ 提交于 2019-12-07 17:53:53

问题


I have a UIViewController with a UIContainerView inside. Based on whether a certain condition is true, I'd like to programmatically set the container view's embedded view to a different UIViewController. I noticed that you can have only one embed segue to set one UIViewController, so is there a way to get this done?

I tried setting my container view as an outlet, but I could not find any methods that set an embedded UIViewController. Any advice on how to get started with this would be appreciated.


回答1:


I think your idea is wrong if you meant swapping UIViews of UIViewController (hope I understood your conception).

A UIViewController should have 1 designed UIView and should manage the values of that view. As you said, you can use Containers, however you should add then UIViewController with it's view, so there is an instance which manages this view. Your first UIViewController should only add/remove that ChildViewController.

So I'd advise:

Implement a category on UIViewController and add following methods:

- (void)displayContentController:(UIViewController *)content {
    [self addChildViewController:content];
    content.view.frame = [[UIScreen mainScreen] bounds];
    [self.view addSubview:content.view];
    [content didMoveToParentViewController:self];
}

- (void)hideContentController:(UIViewController *)content {
    [content willMoveToParentViewController:nil];
    [content.view removeFromSuperview];
    [content removeFromParentViewController];
}

Create AViewController and BViewController. In AViewController call (viewDidLoad?):

BViewController *bViewController = [[BViewController alloc] init];
[self displayContentController:bViewController];

In BViewController manage the view of this controller. AViewController should only manage when to show BViewController and when to hide it.

If I misunderstood your question please comment it, I'll delete this answer.




回答2:


Inspired by Vive's answer:

extension UIViewController {

    func showContentController(_ controller: UIViewController, containerView: UIView? = nil) {
        controller.willMove(toParentViewController: self)
        self.addChildViewController(controller)
        controller.view.frame = (containerView ?? self.view).frame
        (containerView ?? self.view).addSubview(controller.view)
        controller.view.autoPinEdgesToSuperviewEdges()
        controller.didMove(toParentViewController: self)
    }

    func hideContentController(_ controller: UIViewController) {
        controller.willMove(toParentViewController: nil)
        controller.removeFromParentViewController()
        controller.view.removeFromSuperview()
        controller.didMove(toParentViewController: nil)
    }
}


来源:https://stackoverflow.com/questions/30475235/ios-programmatically-set-a-uicontainerviews-embedded-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!