How to scale a TabBarItem Image

馋奶兔 提交于 2019-12-25 12:40:08

问题


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;

回答1:


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

i.e. icon1@2x.png

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.




回答2:


I feel that you should be using a UIToolBar or UISegmentedControl and not a UITabBar.

UITabBar's should usually be used with a UITabBarController to manage it and should be used to switch app "mode" as Apple puts it. This means the TabBarController should be the rootViewController (an exception would be if you add a login view before the app starts up as a tab bar app).

this thread might also help



来源:https://stackoverflow.com/questions/4803836/how-to-scale-a-tabbaritem-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!