标签栏按钮颜色在Swift 3?

win*_*ton 9 storyboard uitabbarcontroller ios swift swift3

在Swift 2中,我在Storyboard中使用了用户定义的运行时属性,并使用了tintColor的关键路径来更改标签栏项目图标颜色.但是,看起来tintColor已经被Swift 3删除了.如何更改Swift 3中标签栏控制器中标签栏项目的选定颜色?

谢谢!

编辑:附加截图

在此输入图像描述

Sam*_*m_M 11

使用tabBarItem.setTitleTextAttributes更改个人物品栏的文本颜色.
把它放在viewDidLoad每个标签的方法中:

self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red()], for:.selected)
Run Code Online (Sandbox Code Playgroud)

要一起更改图标和文本色调颜色,一个简单的解决方案是在每个选项卡的viewWillAppear方法中更改tabBar色调颜色:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.tabBar.tintColor = UIColor.red()
}
Run Code Online (Sandbox Code Playgroud)

更改图像色调颜色的另一种解决方案是为UIImage创建扩展,并使用它来使用自定义色调更改所选图像:

extension UIImage {
    func tabBarImageWithCustomTint(tintColor: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let context: CGContext = UIGraphicsGetCurrentContext()!

        context.translate(x: 0, y: self.size.height)
        context.scale(x: 1.0, y: -1.0)
        context.setBlendMode(CGBlendMode.normal)
        let rect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)

        context.clipToMask(rect, mask: self.cgImage!)

        tintColor.setFill()
        context.fill(rect)

        var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        newImage = newImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        return newImage
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此代码更改所选图像:

self.tabBarItem.selectedImage = self.tabBarItem.selectedImage?.tabBarImageWithCustomTint(tintColor: UIColor.red())
Run Code Online (Sandbox Code Playgroud)