Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContainerView with multiple embed segues

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.

like image 255
Steve Haley Avatar asked Aug 23 '13 16:08

Steve Haley


4 Answers

No, there is no way to have multiple embed segues to one container view. One way to do all the setup in IB, would be to make the embedded controller a UITabBarController (with the tab bar hidden). You can then have as many controllers in the tabs as you want, and switch to them in code using the selectedIndex property of UITabBarController.

like image 123
rdelmar Avatar answered Nov 17 '22 07:11

rdelmar


Yes, I was able to achieve what you're looking for inspired by @rdelmar post. What you need to do is to embed a UITabBarViewController into your container view. Then you programmatically choose which controller you like to present. You might also like to hide the tab bar.

Container view indirectly containing more views

If you want you can also hide the tab bars seen in the storyboard file

You can choose the view controller you want to present by subclassing the UITabBarController:

override func viewDidLoad() {
    super.viewDidLoad()
    self.selectedIndex = 1
}

You can hide the tab bar in your view controller by calling self.tabBarController?.tabBar.isHidden = true in viewDidLoad().

like image 34
Andrej Avatar answered Nov 17 '22 07:11

Andrej


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!

like image 15
UberJason Avatar answered Nov 17 '22 06:11

UberJason


I found this wonderful article that explained exactly how to do it: http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers

you get your container and can call any view controller behind, there is a bit of set up to have everything linked but once done, you get a still useable storyboard.

like image 14
Kanjiroushi Avatar answered Nov 17 '22 05:11

Kanjiroushi