我已经阅读了别人的问题和答案,但没有为我找到有效的答案。
我使用下面的代码将渐变添加到使用层的UIButton:
let gradient = CAGradientLayer()
gradient.colors = [UIColor.black.cgColor, UIColor.gray.cgColor]
gradient.locations = [0.0, 1.0]
myButton.setNeedsLayout() //I added this line then change next line to frame
gradient.frame = myButton.frame //I t