【发布时间】:2013-02-15 13:57:40
【问题描述】:
我正在以编程方式创建一个 UITabBar。这是代码,
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"Item 1" image:[UIImage imageNamed:@"dashboard"] tag:1];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Item 2" image:[UIImage imageNamed:@"documents"] tag:2];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Item 3" image:[UIImage imageNamed:@"mail"] tag:3];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"Item 4" image:[UIImage imageNamed:@"packages"] tag:4];
NSArray *tabbaritems = [[NSArray alloc] initWithObjects:item1, item2, item3, item4, nil];
CGRect bounds = [[UIScreen mainScreen] bounds];
UITabBar *tbbar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 411, bounds.size.width, 49)];
[tbbar setItems:tabbaritems];
[self.view addSubview:tbbar];
- (BOOL)shouldAutorotate
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
//update UITabBar width
}
else {
//update UITabBar width
}
}
我有 2 个问题。如您所见,我已将 CGFloat y 值硬编码为 411。这在纵向模式下的 3.5 英寸屏幕上看起来不错。但是你可以想象的问题是,如果我在 4 英寸的设备上运行它,标签栏会出现在屏幕底部的上方。如果我旋转设备(不管屏幕大小),在横向模式下,标签栏会向下推,因此它根本不会出现在屏幕上。
- 如何动态设置 UITabBar 的位置 它以任何旋转方式运行的屏幕,它总是看起来固定在 屏幕底部?
另一个问题是宽度。当第一次创建 UITabBar 实例时,我已经使用这条线 bounds.size.width 设置它。
- 如何在屏幕旋转时更新它以使其扩展填充 整个屏幕的宽度?
【问题讨论】:
标签: ios objective-c width uitabbar screen-rotation