15

我是 iphone 开发的新手,我需要在标签栏中放置一个自定义图像图标。但它只显示默认颜色(黑色和蓝色)。帮助我克服这个错误朋友......?

4

5 回答 5

23

在应用程序中添加图像,

并参见附图

在此处输入图像描述

于 2012-12-21T13:40:00.823 回答
6

在 viewDidLoad 中使用此代码

UIImage *selectedImage0 = [UIImage imageNamed:@"selected.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"unselected.png"];

UITabBarItem *item0 = [tabBar.items objectAtIndex:0];

[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
于 2012-12-21T10:17:25.627 回答
4

在 AppDelegate 中使用以下代码

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions
{


UIViewController *viewController1 = [[ViewFirstViewController alloc] init];

UIViewController *viewController2 = [[ViewSecondViewController alloc] init];
UIViewController *viewController3 = [[ViewThirdViewController alloc] init];
UIViewController *viewController4 = [[ViewFourthViewController alloc] init];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3,viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];


imgTab = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,50)];
self.tabBarController.tabBar.tag=10;
[self.tabBarController.tabBar addSubview:imgTab];
self.tabBarController.delegate = self;

imghome=[[UIImageView alloc]initWithFrame:CGRectMake(0.0,00.0,80.0,50.0)];
imghome.image=[UIImage imageNamed:@"dressup_active.png"];
[imgTab addSubview:imghome];

imgQuiz=[[UIImageView alloc]initWithFrame:CGRectMake(80.0,00.0,81.0,50.0)];
imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
[imgTab addSubview:imgQuiz];

imgtTW=[[UIImageView alloc]initWithFrame:CGRectMake(161.0,00.0,80.0,50.0)];
imgtTW.image=[UIImage imageNamed:@"greetings.png"];
[imgTab addSubview:imgtTW];

imgGuest=[[UIImageView alloc]initWithFrame:CGRectMake(241.0,00.0,80.0,50.0)];
imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];
[imgTab addSubview:imgGuest];

}

标签栏控制器委托方法

- (void)tabBarController:(UITabBarController *)tabBarControllers didSelectViewController:(UIViewController *)viewController
{

NSLog(@"%i",tabBarControllers.selectedIndex);
if (tabBarControllers.selectedIndex == 0)
{

    imghome.image=[UIImage imageNamed:@"dressup_active.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 1)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree_active.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 2)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings_active.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 3)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_active.png"];

}



}
于 2012-12-21T11:13:19.227 回答
4

您可以将此代码片段用于 ios7:

UIImage *iconBoxOffice = [UIImage imageNamed:@"box-office.png"];
UIImage *selectedIconBoxOffice = [UIImage imageNamed:@"selected-box-office.png"];
UIImage *iconDvds = [UIImage imageNamed:@"dvds.png"];
UIImage *selectedIconDvds = [UIImage imageNamed:@"dvds.png"];

UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];

[item0 initWithTitle:@"Box Office" image:iconBoxOffice selectedImage:selectedIconBoxOffice];
[item1 initWithTitle:@"Dvds" image:iconDvds selectedImage:selectedIconDvds];

如果您有更多选项卡,您可以继续添加更多项目。*不要忘记将图像添加到您的项目中 *这是以编程方式完成的,而不是使用情节提要

于 2014-06-15T17:04:42.587 回答
2

如果您想向标签栏添加自定义图像(图标和背景),而不仅仅是更改蓝色图标:

将以下代码添加到您的第一个 UIView .m 文件中。如果您从名为 FirstViewController.m 的“选项卡式应用程序”项目重新开始,请不要忘记将图像添加(并设置复选标记“将项目复制到目标组的文件夹”)到您的项目并为图像选择更好的名称。

- (void)viewDidLoad
{
  [super viewDidLoad];

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

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

  UITabBar *tabBar = self.tabBarController.tabBar;
  UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
  UITabBarItem *item1 = [tabBar.items objectAtIndex:1];

  [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
  [item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
}

您可以在Ray Wenderlichs页面上找到有关此解决方案的更多信息以及大量其他优秀教程。

于 2012-12-21T10:54:47.020 回答