简体   繁体   中英

change inactive color of the tab item

I was able to change the active color of my tab bar using this answer

无效颜色的选项卡栏

my problem is i dont know how to change the inactive ones like the friends and search tab.I want to change it something like red. I have search so much and decided to ask because i coudn't find any solution.Please bear with me since im new to ios development using swift.

I will post what I did to get what i want to achieve.This might also help biggener's like me in the future.

First I have created two images for each icon

Home Icon,Friends Icon and Search Icon each has white and green color 不活动的标签栏图标

Then clicking Assets.xcassets file to add New Image Set 新影像集

Making it sure that the green icons Render As is set to Original Image since this will be the inactive color icon of our tab bar .Here is a sample screen shots for the sample attribute 渲染为原始图像

By setting the Home Item tab bar Icon. Please Note

System Item => Custom
Selected Image => Home_w//Name of the home white icon
Title => Home
Image=> Home_g//Name of the Inactive tabbar item in my case are sets to green icons

在此处输入图片说明

finally overriding Title Color of our tab bar items in AppDelegate.swift .Please note i have used UIColor_Hex_Swift for hex UIColor.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let color:UIColor =  UIColor(rgba: "#026a34") //UIColor(hexString:"026a34")

    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color], forState: .Normal)

    // then if StateSelected should be different, you should add this code
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: .Selected)

    return true
}

Out put(White ones are the selected Image and green ones are the inactive image)

家庭活跃 朋友活跃 搜索有效

Since iOS 10 you can use this to change the tab item colors (you have to have the images rendering as templates):

    UITabBar.appearance().tintColor = .blue
    UITabBar.appearance().unselectedItemTintColor = .gray

You can set the property Render As of your tab bar images within your asset catalog directly. There you have the option to set the property to Default , Original Image and Template Image .

在此处输入图片说明

尝试这个

 self.tabBar.tintColor=[UIColor whiteColor];

可能是您希望搜索具体解决方案的最佳链接。

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