【问题标题】:setTitleTextAttributes doesn't work for UITabBarItem when it is unselected in swiftsetTitleTextAttributes 在 swift 中未选中时不适用于 UITabBarItem
【发布时间】:2015-01-28 17:00:31
【问题描述】:

我正在尝试自定义我的UITabBarController。我将它嵌入到我的UIViewController 中,我还为此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 对标签栏项目没有任何影响。有人可以帮我找出错误在哪里吗?

【问题讨论】:

    标签: ios swift uitabbaritem


    【解决方案1】:

    这是在这种情况下仍然可以放在 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)
    }
    }
    

    【讨论】:

    • 对我来说,barTintColor 和 tintColor 的 tint color 为正常发挥了作用。谢谢
    【解决方案2】:

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

    【讨论】:

    • 我试图在viewdidload中更改控制器中的属性,当1°视图加载时,2°的标签栏项目仍然是ios默认颜色,即灰色,只有当我点击2° 视图其标签栏项目设置为正确的颜色。您能否更详细地解释一下我必须将代码放在哪里?
    • @Chongzl,将代码放在 awakeFromNib 中。 viewDidLoad 不会在除第一个控制器之外的任何控制器上调用,直到您单击它们的选项卡。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多