Can you have a gradient as a button tintColor?

谁说胖子不能爱 提交于 2019-12-13 07:14:23

问题


I have a icon asset in interface builder which I have as my button image. I can change the colour of it using

button.tintColor = UIColor.blueColor()

Can I apply a gradient though to a tintColor property?

UPDATE

For clarity, my png image icon has different shapes, heart, camera, which I have set up as template images. They all have transparent backgrounds.


回答1:


You can insert CAGradientLayer into your button and apply the tintColor.

Another way is to subclass the UIButton, and in drawRect, use CoreGraphics to draw the gradient effect like:

-(void) drawRect:(CGRect)rect
{
    ... 
    // setup up your tint color for startColor, endColor
    drawLinearGradient(context, rect, startColor, endColor);
}

If you have a transparent background, you can use your view's layer's mask property to achieve gradient.



来源:https://stackoverflow.com/questions/34733943/can-you-have-a-gradient-as-a-button-tintcolor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!