多色选项卡栏图标

时间:2016-03-07 19:05:45

标签: ios swift uiimage uitabbarcontroller uitabbar

我正在为一家餐馆开发应用程序。

我有3个项目的标签栏(家庭,食品,购物车)。

当用户在购物车中添加或删除某些内容时,我需要更改购物车标签栏项目的图标(更改图标不成问题)。

我有带推车和红色圆圈的PNG,上面有1到9的数字。这就是问题,图标有两种颜色(红色圆圈和灰色购物车)。

我尝试将色调颜色设置为清除,但它不起作用。

4 个答案:

答案 0 :(得分:1)

请您使用RDVTabBarController

这是示例代码

UIViewController *firstViewController = [[RDVFirstViewController alloc] init];
UIViewController *firstNavigationController = [[UINavigationController alloc]
                                               initWithRootViewController:firstViewController];

UIViewController *secondViewController = [[RDVSecondViewController alloc] init];
UIViewController *secondNavigationController = [[UINavigationController alloc]
                                                initWithRootViewController:secondViewController];

UIViewController *thirdViewController = [[RDVThirdViewController alloc] init];
UIViewController *thirdNavigationController = [[UINavigationController alloc]
                                               initWithRootViewController:thirdViewController];

RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init];
[tabBarController setViewControllers:@[firstNavigationController, secondNavigationController,
                                       thirdNavigationController]];
self.viewController = tabBarController;


UIImage *finishedImage = [UIImage imageNamed:@"tabbar_selected_background"];
UIImage *unfinishedImage = [UIImage imageNamed:@"tabbar_normal_background"];
NSArray *tabBarItemImages = @[@"first", @"second", @"third"];

RDVTabBar *tabBar = [tabBarController tabBar];

[tabBar setFrame:CGRectMake(CGRectGetMinX(tabBar.frame), CGRectGetMinY(tabBar.frame), CGRectGetWidth(tabBar.frame), 63)];

NSInteger index = 0;
for (RDVTabBarItem *item in [[tabBarController tabBar] items]) {
    [item setBackgroundSelectedImage:finishedImage withUnselectedImage:unfinishedImage];
    UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",
                                                  [tabBarItemImages objectAtIndex:index]]];
    UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
                                                    [tabBarItemImages objectAtIndex:index]]];
    [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];

    index++;
}

答案 1 :(得分:0)

目标C:

将其放入AppDelegate.m方法<{1}}

application didFinishLaunchingWithOptions

Swift 2.1:

将其放入UIImage *selectedImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME_FOR_SELECTED"]; [[UITabBar appearance] setSelectionIndicatorImage:selectedImage]; 方法<{1}}

AppDelegate.swift
  

图片名称application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?),尺寸为let selectedImage : UIImage = UIImage(named: "YOUR_IMAGE_NAME_FOR_SELECTED")! UITabBar.appearance().selectionIndicatorImage = selectedImage 像素

答案 2 :(得分:0)

使图像的渲染模式始终是原始的? vc.tabBarItem.image = UIImage(named:"icon")?.imageWithRenderingMode(.AlwaysOriginal)

答案 3 :(得分:0)

您可能希望查看UITabBarItem的{​​{1}}属性,这是您尝试实现的目标的推荐方式(可能是用户篮子的数量)。

您可以看到它的一个示例是App Store:它看起来像您可以在“更新”标签栏项目上安装的更新数量。