首页 > 解决方案 > 如何在标签栏色调颜色中设置 2 种不同的颜色?

问题描述

我有一个带有图像和标题的标签栏。如何将选定的标签栏项目图像设置为渐变色?

当前的

在此处输入图像描述

预期结果和原始选择图像

在此处输入图像描述

到目前为止我做了什么: -

- (void)viewDidLoad {
    [super viewDidLoad];

    [[UITabBar appearance] setTintColor:ThemeBlueColor];
}

有什么方法可以设置渐变色[[UITabBar appearance] setTintColor:ThemeBlueColor];吗?请帮忙。谢谢你。

标签: iosobjective-cuitabbarcontroller

解决方案


我确实已经更改了标签栏按钮属性,例如标题颜色以及使用目标 c 代码选择和取消选择的图像,并提供了* 使用 Swiftify* 转换的代码

在 tabBarController 的第一个 ViewController 的 viewDidLoad() 中调用这个方法,你就可以开始了

请看一看 - :

//  Converted with Swiftify v1.0.6472 - https://objectivec2swift.com/
func setTabBarSelectedDeselectedIconsAndTitleColor() {
    let recentItem = tabBarController?.tabBar.items[0] as? UITabBarItem
    recentItem?.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .normal)
    recentItem?.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .selected)
    recentItem?.image = UIImage(named: "home_unselect_icon.png")?.withRenderingMode(.alwaysOriginal)
    recentItem?.selectedImage = UIImage(named: "home_select_icon.png")?.withRenderingMode(.alwaysOriginal)
    recentItem = tabBarController?.tabBar.items[1]
    recentItem.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .normal)
    recentItem.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .selected)
    recentItem.image = UIImage(named: "kid_location_unselect_icon.png")?.withRenderingMode(.alwaysOriginal)
    recentItem.selectedImage = UIImage(named: "kid_location_select_icon.png")?.withRenderingMode(.alwaysOriginal)
}

类似的objective-c等价物-:

-(void)setTabBarSelectedDeselectedIconsAndTitleColor{
    UITabBarItem *recentItem = self.tabBarController.tabBar.items[0];
    [recentItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:91.0f/255.0f green:46.0f/255.0f blue:224.0f/255.0f alpha:1.0f]} forState:UIControlStateNormal];
    [recentItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:91.0f/255.0f green:46.0f/255.0f blue:224.0f/255.0f alpha:1.0f]} forState:UIControlStateSelected];
    recentItem.image = [[UIImage imageNamed:@"home_unselect_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    recentItem.selectedImage = [[UIImage imageNamed:@"home_select_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    recentItem = self.tabBarController.tabBar.items[1];
    [recentItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:91.0f/255.0f green:46.0f/255.0f blue:224.0f/255.0f alpha:1.0f]} forState:UIControlStateNormal];
    [recentItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:91.0f/255.0f green:46.0f/255.0f blue:224.0f/255.0f alpha:1.0f]} forState:UIControlStateSelected];
    recentItem.image = [[UIImage imageNamed:@"kid_location_unselect_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    recentItem.selectedImage = [[UIImage imageNamed:@"kid_location_select_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

推荐阅读