2011-01-26 76 views
0

我想在我的UIViewController中添加UITabBar,我不想使用UITabBarController,因为我需要将此视图控制器放到导航控制器中。如何缩放TabBarItem图片

一切都很好,只是我的UITabBarItem图像不能缩放以适合要正确显示的项目大小。

如何解决这个问题?

下面是一些代码:

UITabBar *myTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0.0, barHeight, screenBounds.size.width, 50.0)]; 
myTabBar.opaque = YES; 

UITabBarItem *barItem1 = [[UITabBarItem alloc] initWithTitle:@"title1" image:[UIImage imageNamed:@"icon1.png"] tag:1]; 
UITabBarItem *barItem2 = [[UITabBarItem alloc] initWithTitle:@"title2" image:[UIImage imageNamed:@"icon2.png"] tag:2]; 
UITabBarItem *barItem3 = [[UITabBarItem alloc] initWithTitle:@"title3" image:[UIImage imageNamed:@"icon3.png"] tag:3]; 
UITabBarItem *barItem4 = [[UITabBarItem alloc] initWithTitle:@"title4" image:[UIImage imageNamed:@"icon4.png"] tag:4]; 

NSArray *tbItems = [NSArray arrayWithObjects:barItem1, barItem2, barItem3, barItem4, nil]; 
myTabBar.items = tbItems; 

回答

3

你应该你的TabBar图像保存为42x42。然后,以文件的名称和@ 2x指令。

[email protected]

我们这样做,这样的图标在对iPhone 4/iPod Touch的4G 42x42像素加载,但按比例缩小到21×21的旧设备。

这会节省您很多时间。另请注意,UITabBar只关心图像的alpha通道。所以单色图像是一个好主意。这将节省空间。

0

我觉得你应该使用UIToolBar或UISegmentedControl而不是UITabBar。

UITabBar通常应该与UITabBarController一起使用来管理它,并且应该用于Apple切换应用程序“模式”。这意味着TabBarController应该是rootViewController(如果您在应用程序启动之前添加登录视图作为标签栏应用程序,则会出现异常)。

this thread might also help