【问题标题】:colour icon in UITabbar item objective-cUITabbar项目objective-c中的颜色图标
【发布时间】:2013-03-25 03:35:52
【问题描述】:

我有一个带有 3 个项目的 UITabbar 控制器,我想要颜色图标而不是基于 Gary 的图标,

能否请您给我一些提示,告诉我如何在 tababr 中有颜色图标,

这是我的代码:

self.title = @"test";

   self.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title image:[UIImage 
 imageNamed:@"test"] tag:0];

通常test是带有彩色图片的icone,但在UITabbar中它只是Gary,

提前致谢!

【问题讨论】:

标签: ios objective-c uitabbarcontroller uicolor uitabbaritem


【解决方案1】:

使用此代码:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"test"] 
withFinishedUnselectedImage:[UIImage imageNamed:@"test"]];

【讨论】:

    【解决方案2】:

    您需要构建自己的 TabBarController。根据 Apple 文档关于“此类 [UITabBarController] 不适用于子类化”的问题。 UITabBarItem 上的文档说,当您为标签栏提供图像时,“标签栏上显示的图像来自此图像”。因此,您提供给标签栏的任何图像都会被处理以使其符合标签栏图像的“正常”外观。

    因此,您可以构建一个带有一些 UIButton 作为子视图的 UIViewController,然后以这种方式管理整个外观。

    【讨论】:

      【解决方案3】:

      您必须为所有 3 个选项卡创建 2 个图像,其中一个未选中,第二个已选中。

      之后在您的 appdelegate.m 文件中写入以下代码

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
             self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
             // Override point for customization after application launch.
             self.searchArray = [[NSMutableArray alloc]init];
             self.tabBarController = [[[UITabBarController alloc] init] autorelease];
      
             viewController1 *view1 = [[[viewController1 alloc] initWithNibName:@"viewController1" bundle:nil] autorelease];
             view1.tabBarItem.image = [UIImage imageNamed:@"tab-selected-1"];
             view1.tabBarItem.title = @"Title1";           
      
             viewController2 *view2 = [[[viewController2 alloc] initWithNibName:@"viewController2" bundle:nil] autorelease];
             view2.tabBarItem.image = [UIImage imageNamed:@"tab-selected-2"];
             view2.tabBarItem.title = @"Title2";
      
             viewController3 *view3 = [[[viewController3 alloc] initWithNibName:@"viewController3" bundle:nil] autorelease];
             view3.tabBarItem.image = [UIImage imageNamed:@"tab-selected-3"];
             view3.tabBarItem.title = @"Title3";
      
      
      
             self.tabBarController.viewControllers = [NSArray arrayWithObjects:view1, view2, view3, nil];
      
             UITabBarItem *tabBarItem1 = [[self.tabBarController.tabBar items] objectAtIndex:0];
             [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"tab-selected-1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-unselected-1.png"]];
      
      
             UITabBarItem *tabBarItem2 = [[self.tabBarController.tabBar items] objectAtIndex:1];
             [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"tab-selected-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-unselected-2.png"]];
      
             UITabBarItem *tabBarItem3 = [[self.tabBarController.tabBar items] objectAtIndex:2];
             [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"tab-selected-3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-unselected-3.png"]];
      
      
             return YES;
      }
      

      试试这个,你的问题就解决了。祝你好运

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-03
        相关资源
        最近更新 更多