Navigation Controller Stack Navigation
I would like to add a label above the tab bar in my app. I would like it to look like this:
Unfortunately, the navigation controller stack can get very complicated. Here’s a screenshot of the side view of one from Xcode’s view hierarchy debugger:
That’s a lot of layers. Thankfully, Xcode has an easier way of looking at them:
Here’s the purpose of each of these:
UIWindow: The main window of the app. It contains the visible content, delivers touch events to views and other application objects, and helps to handle orientation changes.
UILayoutContainerView: Private Apple class, contains view controllers and determines the layout of its children (in this case the transition view and the tab bar).
UITransitionView: Another private Apple class, this view is constrained by the LayoutContainerView and handles transitions between views by adding their wrapper.
UIViewControllerWrapperView: This view does what it sounds like, even if the purpose of it doesn’t seem obvious. View controller’s are not supposed to have their display properties affected by other view controllers (for various reasons) but the wrapper can have its properties changed.
UILayoutContainerView: Same purpose as the first one, but this is for children of the navigation controller rather than the tab bar controller.
UINavigationTransitionView: Same purpose as the UITransitionView, but this handles Navigation Controller transitions.
UIViewControllerWrapperView: The Navigation Controller’s wrapper view.
UIView: Finally one of my views.
Now, back to that label. Because of autolayout issues, I would prefer not to use device coordinates. Therefore, I will need to traverse the navigation controller stack in order to do so. I can try to mess around with the private class UILayoutContainerView (assuming this stack is consistent) or I can just do this:
with this call:
Basically, this method checks if it finds a tab bar among the subviews, and if not, go one level deeper. So, yeah, I guess all of that navigation stack wasn’t really necessary for me. But learning is fun, right?
Big thanks to the author of this blog: http://stablekernel.com/blog/view-controller-containers-part-i/