简体   繁体   中英

setTitleTextAttributes doesn't work for UITabBarItem when it is unselected in swift

I am trying to custom my UITabBarController . I have it embedded in my UIViewController , I also created a class for this UITabBarController .

override func viewDidLoad() {
    super.viewDidLoad()

    //custom tab bar
    self.tabBar.barTintColor = UIColor(red: 0.0/255.0, green: 102.0/255.0, blue: 153.0/255.0, alpha: 1)
    self.tabBar.tintColor = UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)
    self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
    self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Disabled)

    for item in self.tabBar.items as [UITabBarItem]
    {
        item.image = item.selectedImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    }


    // Do any additional setup after loading the view.
}

setTitleTextAttributes doesn't have any effects on the tab bar item. Can somebody please help me to find where the error is?

Here is the code that works in this case to put still in the UITabBarController:

override func viewDidLoad() {
super.viewDidLoad()

//custom tab bar
self.tabBar.barTintColor = UIColor(red: 0.0/255.0, green: 102.0/255.0, blue: 153.0/255.0, alpha: 1)
self.tabBar.tintColor = UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)

for item in self.tabBar.items as [UITabBarItem]
{
    item.image = item.selectedImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
    item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Disabled)
    item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)], forState:UIControlState.Selected)
}
}

选项卡栏项目属于各个子视图控制器,因此您需要更改这些控制器中的属性,而不是选项卡栏控制器。

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