【问题标题】:Colorize image, take the wrong color swift为图像着色,迅速取错颜色
【发布时间】:2016-09-20 23:29:29
【问题描述】:

我想为 imageView 中的图像着色。之后我想将此图像设置为按钮。

    let imageView:UIImageView = UIImageView()

    if let myImage = UIImage(named: "ic_star") {
        let tintableImage = myImage.imageWithRenderingMode(.AlwaysTemplate)
        imageView.image = tintableImage
    }

    imageView.tintColor = UIColor.redColor() 

    bestListButton.setImage(imageView.image, forState: .Normal)

这是一个 sn-p,应该可以工作,但它总是将我的按钮图像着色为蓝色。 调试器说颜色没有改变。模拟器将其更改为蓝色。

好像和我的图片背景色没有关系。

编辑和(破解)解决方案 好吧,这很奇怪。蓝色是因为在我的界面构建器中是 tintColor blue。 我把它改成了白色。现在我的星星是白色的。

但是:当我删除我的代码时,图像会变回黑色。所以颜色的变化似乎只是结合了界面生成器和代码。

编辑 2 以编程方式解决: bestListButton.tintColor = UIColor.redColor()

【问题讨论】:

    标签: swift image button colors imageview


    【解决方案1】:

    你的图片是 PNG 文件吗?

    试试:

    let imageView:UIImageView = UIImageView()
    
    if let myImage = UIImage(named: "ic_star") {
        imageView.image = imageView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    }
    
    imageView.tintColor = UIColor.redColor()
    
    bestListButton.setImage(imageView.image, forState: .Normal)
    

    【讨论】:

    • 是的。我的图像是来自谷歌材料设计的官方 gylphicon。你的代码会抛出一个错误,因为我的 imageView 是空的......或者不是空的但它没有图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2011-11-14
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多