ContainerView with multiple embed segues

2020-05-12 11:47发布

Is there a way to have a single ContainerView with multiple embed segues? The aim is for a ContainerView to hold a few different ViewControllers depending on what buttons have been pressed; only one is going to be visible at a time. I want to use embed segues so that in Interface Builder the storyboards are automatically shown at the same size as the ContainerView.

I realise that I can manually resize the other ViewControllers in InterfaceBuilder, but I want the automatic sizing provided by the embed segue. If another way of doing that is available, that would be fine too. Not having the views load on viewDidLoad is fine - as mentioned earlier, the shown ViewController can change depending on the buttons pressed.

7条回答
Root(大扎)
2楼-- · 2020-05-12 12:48

I recognize this question is a bit old, but I wanted to answer in case you're still looking or other people find this. I had a similar issue and I worked around it.

In short, you'll have three layers:
- an external view controller ("ExternalViewController")
- a view controller manager ("ViewControllerManager")
- the child view controllers you actually want to be switching between ("ChildViewController")

Use a container view in ExternalViewController with an embed segue to the ViewControllerManager. ViewControllerManager itself would then hold other ChildViewControllers programmatically as described in this Apple documentation, specifically the section on adding and removing a child.

When you add a child view controller, set its frame to be the same as the ViewControllerManager's frame (since you're doing this inside the ViewControllerManager, set the child's frame equal to self.view.frame). You will also need some logic and an external control to do the switching inside of ExternalViewController, of course.

Hope this helps!

查看更多
登录 后发表回答