UIButton在突出显示时带有圆角和自定义backgroundImage

时间:2014-10-17 17:58:13

标签: ios swift uibutton

问题是突出显示时我的按钮无法正确显示。我希望背景图像是蓝色的,而不是不透明的。我使用的代码如下所示。

self.button.layer.borderWidth = 1.0
self.button.layer.borderColor = self.button.tintColor?.CGColor
self.button.layer.cornerRadius = 5.0
self.button.layer.masksToBounds = true

self.button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
self.button.setBackgroundImage(self.imageWithColor(self.button.tintColor!, size: self.button.bounds.size), forState: UIControlState.Highlighted)

获取带有颜色的图像的方法。

func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(size, true, 1.0)
    color.setFill()
    var bounds = CGRectMake(0, 0, size.width, size.height)
    UIRectFill(bounds)

    let image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext()

    return image;
}

0 个答案:

没有答案