如何以编程方式定位TabBar

时间:2009-11-23 17:42:26

标签: iphone

我正在以编程方式创建一个视图控制器,它是UITabBarDelegate并包含UITabBar

UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 200, 320, 49)];
[self.view addSubview:tabBar];

我真的不想在屏幕分辨率发生变化时硬编码帧值,或者我在不同高度的不同上下文中使用视图,例如。

我隐约知道我可以创建没有这些值的标签栏,并探测窗口和标签栏的高度和宽度来计算所需的值,但我很难实现这一点。我想要实现的是标准宽度和高度的标签栏正确位于封闭视图的底部。

1 个答案:

答案 0 :(得分:0)

也许是这样的:

UITabBar *tabBar = [[UITabBar alloc] init]; //init with default width and height
tabBar.frame = CGRectMake(0,
                          self.view.frame.size.height - tabBar.frame.size.height,
                          self.view.frame.size.width,
                          tabBar.frame.size.height)]; //place it at the bottom of the view
[self.view addSubview:tabBar];
[tabBar release];

编辑:

我已经意识到新实例化的UITabBar的大小为零。可能你最好的选择是使用UITabBarController,因为它已经相应地调整了UITabBar的大小:

UITabBarController *tabBarController = [[UITabBarController alloc] init];
[self.view addSubview: tabBarController.view];
相关问题