swift - GradientLayer 显示在按钮外

标签 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

最佳答案

再添加一行:

button.clipsToBounds = true

你的结果将是:

enter image description here

关于swift - GradientLayer 显示在按钮外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967744/

相关文章:

swift 如何使渐变层从不同的位置开始

json - 如何将可映射数组转换为 JSON?

swift - 从不同的 ViewController 更改 UIButton

swift - 以编程方式将按钮放在一起

ios - 如何为 UIButton 设置自定义渐变背景?

ios - 动态创建时 UIButton 不突出显示

ios - Swift - 使用 animateWithDuration 淡化 CAGradientLayer

ios - 使用 UIScrollView 的渐变背景颜色

swift - 使用 Swift 将位置发布到 iCloud 数据库

ios - 当枚举大小写具有相同的关联值时,避免重复的代码行?