如何缩放 TabBarItem 图像
我想在我的 UIViewController
中添加一个 UITabBar
,我不想使用 UITabBarController
因为我需要将此视图控制器推送到导航中控制器。
一切都很好,除了我的 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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将 TabBar 图像保存为 42x42。然后,在文件名中添加 @2x 指令。
即 [email protected]
我们这样做该图标在 iPhone 4/iPod Touch 4G 上以 42x42 像素加载,但在旧设备上则缩小至 21x21。
这将为您节省大量时间。另请注意,UITabBar 只关心图像的 Alpha 通道。所以单色图像是个好主意。这将节省空间。
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.
我觉得你应该使用 UIToolBar 或 UISegmentedControl 而不是 UITabBar。
UITabBar 通常应该与 UITabBarController 一起使用来管理它,并且应该用于切换应用程序“模式”,正如 Apple 所说的那样。这意味着 TabBarController 应该是 rootViewController(例外情况是,如果您在应用程序作为选项卡栏应用程序启动之前添加登录视图)。
此帖子也可能有帮助
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