Y-coordinate of UITabBar subview
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 UITabBar
to 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 viewDidLoad
alloc/init the tab-bar as normal, and position it as follows:
tabBar.frame = CGRectMake(self.view.frame.size.height-tabBar.frame.size.height, 0,
tabBar.frame.size.height, tabBar.frame.size.height);
[self.view addSubview:tabBar];
...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:
tabBar.autoresizingMask = UIViewAutoresizingMaskTopMargin;
...which will effectively lock the bar to the bottom of the superview.
精彩评论