【问题标题】:Tab Bar Button color in Swift 3?Swift 3中的标签栏按钮颜色?
【发布时间】:2017-01-17 00:26:53
【问题描述】:

在 Swift 2 中,我在 Storyboard 中使用了一个用户定义的运行时属性,其键路径为 tintColor 来更改选项卡栏项目图标的颜色。但是,Swift 3 似乎删除了 tintColor。如何在 Swift 3 的标签栏控制器中更改标签栏项目的选定颜色?

谢谢!

编辑:附上截图

【问题讨论】:

  • 在 Xcode 8 swift 3 中试过,效果很好。您是为标签栏本身还是在其中一个栏项目上设置色调颜色?
  • 感谢您的回复!我正在尝试设置单个项目的颜色。我刚刚附上了截图。

标签: ios swift storyboard uitabbarcontroller swift3


【解决方案1】:

与 Swift 3 一样的最新代码是

extension UIImage {

    func tabBarImageWithCustomTint(tintColor: UIColor) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let context: CGContext = UIGraphicsGetCurrentContext()!

        context.translateBy(x: 0, y: self.size.height)
        context.scaleBy(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.clip(to: rect, mask: self.cgImage!)

        tintColor.setFill()
        context.fill(rect)

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

        newImage = newImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        return newImage
    }
}

【讨论】:

    【解决方案2】:

    使用tabBarItem.setTitleTextAttributes 更改各个条形项目的文本颜色。
    把这个放在每个选项卡的viewDidLoad 方法中:

    self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red()], for:.selected)
    

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

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

    改变图像色调颜色的另一种解决方案是为 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
        }
    }
    

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

    self.tabBarItem.selectedImage = self.tabBarItem.selectedImage?.tabBarImageWithCustomTint(tintColor: UIColor.red())
    

    【讨论】:

    • 抱歉,我应该更具体一些。我想改变图标的​​颜色。我已经想出了如何更改文本。谢谢!
    猜你喜欢
    • 2014-12-23
    • 1970-01-01
    • 2011-03-26
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多