iOS8如何设置TabBarItem图片
Posted
技术标签:
【中文标题】iOS8如何设置TabBarItem图片【英文标题】:iOS8 How to set TabBarItem images 【发布时间】:2014-09-25 22:14:31 【问题描述】:ios8 似乎发生了一些变化,现在我的标签栏图标都没有正确显示。大多数情况下,直到标签处于活动状态时它们才会显示:
但有时它们根本不出现,只给我一个蓝色的大框(就像每当我关闭覆盖整个窗口的视图时):
这是我在 iOS8 之前所做的:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"paintbrush-white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"paintbrush-black.png"]];
tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
tabBarItem1.title = @"";
【问题讨论】:
【参考方案1】:如前所述,如果你看一下:
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITabBarItem_Class/index.html#//apple_ref/occ/instm/UITabBarItem/setFinishedSelectedImage:withFinishedUnselectedImage:
您会注意到此方法已被弃用,请尝试更改:
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"paintbrush-white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"paintbrush-black.png"]];
到:
[tabBarItem1 setImage:[[UIImage imageNamed:@"paintbrush-white.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:@"paintbrush-black.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
您也可能对图像大小有问题,这取决于在 iPhone 5 屏幕和 iPhone 6 屏幕上测试 @2x 图像时的图像大小
【讨论】:
【参考方案2】:你试过 setSelectedImage: 吗?
UIImage *image = [UIImage imageNamed:@"img.png"] [tabItem setSelectedImage:image];
它对我有用。
【讨论】:
【参考方案3】:此方法在 iOS 8 中已弃用:
Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal
【讨论】:
我找不到任何说它已被弃用的东西。设置 image 和 selectedImage 属性也不起作用。以上是关于iOS8如何设置TabBarItem图片的主要内容,如果未能解决你的问题,请参考以下文章