【问题标题】:GradientLayer shows outside the buttonGradientLayer 显示在按钮外
【发布时间】: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)

【问题讨论】:

    标签: swift uibutton cagradientlayer


    【解决方案1】:

    只需再添加一行:

    button.clipsToBounds = true
    

    你的结果将是:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多