2

我有UITabBarControllerUITabBar三个UITabBarItem。项目带有故事板。如何为每个设置不同的图像UITabBarItem

我想做这样的事情:

TabBarItem1 -> image1_unselected and image1_selected

TabBarItem1 -> image2_unselected and image2_selected

TabBarItem1 -> image3_unselected and image3_selected

编辑:

可能我写的不准确,但我想设置自定义图像并UITabbar使用自定义图形制作。类似于UITabbar“查找我的朋友”应用程序中的内容。

4

4 回答 4

2

好的,我找到了解决方案。

在我的开头,viewDidLoadfirsTabBarViewController.m 添加了以下代码:

UIImage *selectedImage0 = [UIImage imageNamed:@"barBtn1_selected.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"barBtn1.png"];

UIImage *selectedImage1 = [UIImage imageNamed:@"barBtn2_selected.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:@"barBtn2.png"];

UIImage *selectedImage2 = [UIImage imageNamed:@"barBtn3_selected.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:@"barBtn3.png"];

UITabBar *tabBar = self.tabBarController.tabBar;

UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];

[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2]; 

现在我的问题正是我想要的。

于 2012-07-10T17:07:06.187 回答
0

TabBar图像为 32x32 点或更小,并且由 iOS 自动设置样式以显示在monochromatic配色方案中(无论您选择什么)。在创建标签栏界面艺术时,具有透明背景的简单线条图效果最好。

这是通过首先将图像资源添加到您的项目来完成的。

然后单击TabBarItem您要编辑的,然后转到属性检查器 ( Alt + Cmd + 4)

然后把' Identifier'改成' Custom'

最后,从图像下拉列表中选择您的图像。

于 2012-07-05T10:38:39.677 回答
0

murzynpl答案是正确的,但是您也可以通过AppDelegate.mdidFinishLaunchingWithOptions方法中添加以下代码来做到这一点

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];

tabBarItem1.title = @"Home";
tabBarItem2.title = @"Favorites";

NSString *tabOneImage = [[NSString alloc] init];
NSString *tabOneImageSelected = [[NSString alloc] init];
NSString *tabTwoImage = [[NSString alloc] init];
NSString *tabTwoImageSelected = [[NSString alloc] init];

if ([UIScreen mainScreen].bounds.size.height > 480.0f) {
    tabOneImageSelected = @"home_icon_selected-568h@2x.png";
    tabTwoImageSelected = @"fav_icon_selected-568h@2x.png";
    tabOneImage = @"home_icon-568h@2x.png";
    tabTwoImage = @"fav_icon-568h@2x.png";
}
else {
    tabOneImageSelected = @"home_icon_selected.png";
    tabTwoImageSelected = @"fav_icon_selected.png";
    tabOneImage = @"home_icon.png";
    tabTwoImage = @"fav_icon.png";
}


[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:tabOneImageSelected]
          withFinishedUnselectedImage:[UIImage imageNamed:tabOneImage]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:tabTwoImageSelected]
          withFinishedUnselectedImage:[UIImage imageNamed:tabTwoImage]];
于 2013-09-04T23:59:58.243 回答
0

在每个 viewcontroller.

-> 在initwithnibname....; 方法

self.tabBarItem.image = [UIImage imageNamed:@"image.png"];

于 2012-07-05T10:51:48.467 回答