Embedding a ViewController in another with storyboards using a Container View

Container Views are ssweet! They are great and easy way to embed an existing view controller inside of another view controller while keeping everything clean, in a story board and avoiding code duplication.

All you need to do is:
– drag a container view into your view controllers view in your storyboard
– Xcode will automatically add a new VC for you as the outlet for the new container view, but if you want to use an existing VC then delete that stuff.
– Drag from the container view to the view controller you want to embed to create an ‘embed’ segue between the container view and the embedded view controller. Name the segue
– Now in your parent VC, you can set up the embedded view controller if needed in the prepare for segue method.
– create a UIView * outlet from your parent view controller to the Container View. You may now hide or show that view controller as needed within your parent view controller.

As long as you laid our your view controller correctly with auto layout, the embedded view controller will show correctly inside the parent view controller within a container view, sized however you’d like!



This site uses Akismet to reduce spam. Learn how your comment data is processed.