I’m creating a custom subclass of a UIViewController (without a nib), which I’m pushing onto a UINavigationController stack. Somewhere during the initialization of my UIViewController subclass (loadView? viewDidLoad? init?) I want to add a UITabBar subview to the bottom of the view. The problem is figuring out the Y-coordinate. As far as I can tell, the view gets resized somewhere after loadView, viewDidLoad, and init so I can’t get the resized height in order to calculate the Y-coordinate of the UITabBar.
What is the proper way to figure out the height of the containing view such that I can anchor the UITabBar at the bottom?
What you need to do is set your
UITabBarto the bottom of the view, and then tell it to stay there if the bounds of the superview changes.This isn’t too tricky. For example, in
viewDidLoadalloc/init the tab-bar as normal, and position it as follows:…which will add the bar to the bottom of the view.
So far so good: your problem is when the superview changes height the tabbar doesn’t stay locked to the bottom. To fix this, we set an autoresizing mask:
…which will effectively lock the bar to the bottom of the superview.