ios - 多色标签栏图标

标签 ios swift uiimage uitabbarcontroller uitabbar

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

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

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

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

我尝试将 tint Color 设置为 clear,但没有成功。

最佳答案

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

关于ios - 多色标签栏图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35851882/

相关文章:

ios - 如何在 UICollectionView 上面实现内容 View

ios - SKNode接触其他SKNode识别

Swift Xcode 索引卡住或缓慢

ios - 当其他 View 处于动画状态时关闭键盘

ios - PHAsset 获取将集合的 50% 返回为 nil

iOS通过文件或nsdata将UIImage保存在plist中?

ios - TagListView单选swift 4

ios - 用户位置不会出现在 Xcode 的模拟器中

ios - 图像 mask 并创建新的 UIImage

ios - 移除 KVO 观察器时 APP 崩溃