多色标签栏图标



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

我有包含 3 个项目(家庭、食物、购物车)的标签栏。

当用户在购物车

中添加或删除某些内容时,我需要更改购物车选项卡栏项目的图标(更改图标不是问题)。

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

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

请您可以使用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++;
}

目标 C :

把它放在application didFinishLaunchingWithOptions方法的AppDelegate.m

UIImage *selectedImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME_FOR_SELECTED"];
[[UITabBar appearance] setSelectionIndicatorImage:selectedImage];

斯威夫特 2.1 :

把它放在application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)方法的AppDelegate.swift

let selectedImage : UIImage = UIImage(named: "YOUR_IMAGE_NAME_FOR_SELECTED")!
UITabBar.appearance().selectionIndicatorImage = selectedImage

图像名称的大小"YOUR_IMAGE_NAME_FOR_SELECTED"大小98x98像素

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

你可能

想查看UITabBarItembadgeValue属性,这是你尝试实现的目标的推荐方法(大概是用户购物篮的计数)。

您可以看到它的一个例子是App Store:它看起来像您可以安装的更新数量,标记在"更新"标签栏项上。

最新更新