GradientLayer显示在按钮外部

时间:2017-12-25 10:24:59

标签: swift uibutton cagradientlayer

我正在使用Swift进行开发,并希望制作一个带有渐变图层的圆角按钮。在按钮的角落,渐变图层显示在按钮外部。怎么解决?

这是我为它制作的代码:

    let newLayer = CAGradientLayer()
    newLayer.frame = button.bounds
    newLayer.colors = [UIColor.white.cgColor, UIColor.lightGray.cgColor]
    button.layer.insertSublayer(newLayer, at: 0)

    button.layer.cornerRadius = 10.0
    button.layer.borderWidth = 3.0
    button.layer.borderColor = UIColor.darkGray.cgColor
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOpacity = 1.0
    button.layer.shadowRadius = 5.0
    button.layer.shadowOffset = CGSize.init(width: 3, height: 3)

enter image description here

1 个答案:

答案 0 :(得分:1)

再添加一行:

button.clipsToBounds = true

你的结果将是:

enter image description here