我正在开发一个选项卡式应用程序,我想集成自定义 UITabbar 背景和图标。我已经插入了自定义背景,但图标有问题。
这就是图标的外观:
但是在情节提要中为每个视图添加图标后,我会得到如下灰色图标:
有谁知道为什么图标以这种方式显示?
非常感谢!
我正在开发一个选项卡式应用程序,我想集成自定义 UITabbar 背景和图标。我已经插入了自定义背景,但图标有问题。
这就是图标的外观:
但是在情节提要中为每个视图添加图标后,我会得到如下灰色图标:
有谁知道为什么图标以这种方式显示?
非常感谢!
使用 Xcode 6,为了能够直接从 Storyboard 更改图像,您可以这样做:
@IBDesignable class CustomizedTabBarItem: UITabBarItem {
@IBInspectable var finalImage:UIImage = UIImage() {
didSet {
self.image = finalImage.imageWithRenderingMode(.AlwaysOriginal)
}
}
@IBInspectable var finalSelectedImage:UIImage = UIImage() {
didSet {
self.selectedImage = finalSelectedImage.imageWithRenderingMode(.AlwaysOriginal)
}
}
}
之后,您只需在情节提要中设置图像
我还不能发表评论,所以我会添加作为答案。
由于 iOS 7setFinishedSelectedImage:withFinishedUnselectedImage:
已弃用。
要解决此问题,您可以这样做:
UIImage *selectedImage = [UIImage imageNamed:@"tabbar-highlight"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedImage = [UIImage imageNamed:@"tabbar"];
unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem = [[UITabBarItem alloc] initWithTitle:@"TabBarItem1" image:unselectedImage selectedImage:selectedImage];
//
//OR
//
UIImage *selectedImage = [UIImage imageNamed:@"tabbar-highlight"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedImage = [UIImage imageNamed:@"tabbar"];
unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[yourTabBarItem setImage:unselectedImage];
[yourTabBarItem setSelectedImage:selectedImage];
希望这有助于澄清 iOS7+。
这就是我在我的一个应用程序中执行此操作的方式。在您的 AppDelegatesdidFinishLaunchingWithOptions:
方法中,添加此内容并填写您的图像名称:
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
//tab1
UIImage *selectedImage = [UIImage imageNamed:@"home-tab-selected"];
UIImage *unselectedImage = [UIImage imageNamed:@"home2-tab"];
MyTabBar *tabBar = (MyTabBar *)tabController.tabBar;
UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
[item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
//tab2
selectedImage = [UIImage imageNamed:@"customers-tab-selected"];
unselectedImage = [UIImage imageNamed:@"customers-tab"];
UITabBarItem *item2 = [tabBar.items objectAtIndex:1];
[item2 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
//tab3
selectedImage = [UIImage imageNamed:@"maps-tab-selected"];
unselectedImage = [UIImage imageNamed:@"maps-tab"];
UITabBarItem *item3 = [tabBar.items objectAtIndex:2];
[item3 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
//tab4
selectedImage = [UIImage imageNamed:@"reports-tab-selected"];
unselectedImage = [UIImage imageNamed:@"reports-tab"];
UITabBarItem *item4 = [tabBar.items objectAtIndex:3];
[item4 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
//tab5
selectedImage = [UIImage imageNamed:@"orders-tab-selected"];
unselectedImage = [UIImage imageNamed:@"orders-tab"];
UITabBarItem *item5 = [tabBar.items objectAtIndex:4];
[item5 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
{
// ios 5 code here
[tabBar setBackgroundImage:[UIImage imageNamed:@"tab-bg"]];
}
非常适合我。
Julien 关于 IBInspectable 的回答的更新版本如下
import UIKit
@IBDesignable
class CustomTabBarItem: UITabBarItem {
@IBInspectable var finalImage: UIImage = UIImage() {
didSet {
self.image = finalImage.withRenderingMode(.alwaysOriginal)
}
}
@IBInspectable var finalSelectedImage: UIImage = UIImage() {
didSet {
self.selectedImage = finalSelectedImage.withRenderingMode(.alwaysOriginal)
}
}
}
setFinishedSelectedImage:withFinishedUnselectedImage:
您可以使用on设置标签栏图标的完成图像UITabBarItem
在此处查看文档(在外观下查看)
的默认行为UITabBarItem
是根据您的图标创建一个蒙版,然后应用特殊的蓝色处理作为您的查看。
你也可以保持简单;)
[tabBarItemName setSelectedImage:[UIImage imageNamed:@"image name"]];
您必须在 ViewController.h 中声明 tabBarItemName