简体   繁体   中英

How can I set 2 different color in Tab Bar tint color?

I have a tab bar with image and title. How can I set selected tab bar item image to gradient color?

Current

在此处输入图片说明

Expected Result and original selected image

在此处输入图片说明

What I did so far:-

- (void)viewDidLoad {
    [super viewDidLoad];

    [[UITabBar appearance] setTintColor:ThemeBlueColor];
}

Is there any method to set the gradient color for [[UITabBar appearance] setTintColor:ThemeBlueColor]; ? Please help. Thank you.

I do have changed tabbar button properties like title colour and selected and deselected image with objective c code and provided * converted code with Swiftify* .

call this method in your first ViewController's viewDidLoad() of tabBarController and you are good to go

please have a look - :

//  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)
}

similar objective-c equivalent -:

-(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];
}

您以渐变色获取图像资产,对于选定的图像,只需分别设置该图像或未选择的图像即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM