2011-01-26 73 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