GradientLayer 显示在按钮外

GradientLayer shows outside the button

我在 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)

再加一行:

button.clipsToBounds = true

你的结果将是: