Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

iphone - How to scale a TabBarItem Image

I want to add a UITabBar in my UIViewController, I don't want to use UITabBarController because I need to push this view controller into a navigation controller.

Everything is fine except that my images for UITabBarItem is not scale to fit the item size to be displayed properly.

How to fix this problem?

Here is some code:

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should save your TabBar images as 42x42. Then, in the name of the file ad the @2x directive.

i.e. [email protected]

We do this so that the icon is loaded at 42x42 pixels on the iPhone 4/iPod Touch 4G, but scaled down to 21x21 for older devices.

This is going to save you a lot of time. Also note that the UITabBar only cares about the alpha channel of the image. So single color images are a good idea. This will save space.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...