GradientLayer显示在按钮外部

问题描述 投票:0回答:1

我正在使用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

swift uibutton cagradientlayer
1个回答
1
投票

只需添加一行:

button.clipsToBounds = true

你的结果将是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.