GradientLayer 显示在按钮外

Posted

技术标签:

【中文标题】GradientLayer 显示在按钮外【英文标题】:GradientLayer shows outside the button 【发布时间】:2017-12-25 10:24:59 【问题描述】:

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

【问题讨论】:

【参考方案1】:

只需再添加一行:

button.clipsToBounds = true

你的结果将是:

【讨论】:

以上是关于GradientLayer 显示在按钮外的主要内容,如果未能解决你的问题,请参考以下文章