ContainerViewControllers help you set up fast and easy ViewController hierachies. It can be cumbersome to add viewControllers. The Interface builder is not your friend. Also setting up the viewControllers programatically is also cumbersome. Issues are also common with sizing and re-layouting. This project offers some useful base viewControllers which extend the apple provided container controllers nicely:
- JMContainerViewController offers a simple base class to embed viewControllers.
- JMStackContainerViewController allows you to stack multiple child containers vertically.
- JMDocumentViewController adds a title bar above your content.
Also you can now establish viewController relationships directly with segues by using 'JMEmbedSegue' or 'JMEmbedInDocumetSegue'. There are some manual steps neccessary, however I plan on implementing a interface builder rule to do these steps automatic.
The default container simply adds the view to the container's subviews.
This stack container combines normal ViewControllers and ViewControllers embeded in Document Containers.
The setup of the storyboard. The viewControllers that have been embeded in Document Controllers use a JMEmbedInDocumentSegue while the normal ViewControllers use a JMEmbedSegue.
To run the example project, clone the repo, and run pod install
from the Example directory first.
JMContainerControllers is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'JMContainerControllers'
The document view controller container adds a header view above the contents of the child controller. The child controller uses a navigation item to add a title and subtitle to the header view. The header view might be modified with appearance selectors (including header size, label font, sub-label text color, hairline view color...)
The StackContainerViewController manages its child controllers via a UIStackView. The individual size of a child view can be controlled with
- (CGSize)preferredContentSizeForExpectedContainerSize:(CGSize)size
If setting up the hierachy with a storyboard, set the view controller's class to 'JMStackContainerView' and also set the view's class to 'JMStackContainerView'. The last step has historical reasons and will be changed in future releases.
If setting up the hierachy programatically, you have to use '-setView:' before '-view' is invoked, or override the '-loadView' method and supplement the JMStackContainerView yourself. In future releases this will also be changed.
Interface builder does yet not allow you to perform segues that are executed automatically on viewDidLoad. The default interface builder container segue between a view and a viewController however does this. This is done via SegueTemplates that can be triggered on viewDidLoad. However segue triggers are currently private API, so we only use these for Debugging perposes. When we want to use segues to establish the container relationship between different view controllers, we have to establish the trigger ourself.
Here is how it works:
- Add a 'JMEmbedSegue' or 'JMEmbedInDocumentSegue' segue between your container view controller and your child controller.
- Add a custom object of type 'JMSegueTrigger' and set the identifier property to the respective segue's identifer.
- Add the segue trigger to the outlet collection of segue triggers.
Martin Stähler, martin.staehler@gmail.com
JMContainerControllers is available under the MIT license. See the LICENSE file for more info.