Ios 自定义UI按钮不显示标题
我在swift中为follow按钮创建了一个自定义UI按钮类,但是它没有显示按钮标题 我的代码Ios 自定义UI按钮不显示标题,ios,swift,Ios,Swift,我在swift中为follow按钮创建了一个自定义UI按钮类,但是它没有显示按钮标题 我的代码 @IBDesignable public class GradientButton: UIButton { override public func layoutSubviews() { super.layoutSubviews() layoutGradientButtonLayer() } // MARK: Private private func layoutGradientB
@IBDesignable
public class GradientButton: UIButton {
override public func layoutSubviews() {
super.layoutSubviews()
layoutGradientButtonLayer()
}
// MARK: Private
private func layoutGradientButtonLayer() {
let layer = UIView()
layer.frame = CGRect(x: 191, y: 257, width: 66.6, height: 24)
let gradient = CAGradientLayer()
gradient.frame = CGRect(x: 0, y: 0, width: 66.6, height: 24)
gradient.colors = [
UIColor(red: 0.09, green: 0.85, blue: 0.88, alpha: 1).cgColor,
UIColor(red: 0, green: 0.87, blue: 0.68, alpha: 1).cgColor
]
gradient.locations = [0, 1]
gradient.startPoint = CGPoint(x: 0.78, y: 1)
gradient.endPoint = CGPoint(x: 0.15, y: 1)
self.layer.cornerRadius = 10
self.layer.addSublayer(gradient)
self.clipsToBounds = true
self.setTitle("Follow", for: UIControlState.normal)
}
}
按钮显示为小宽度,并且没有任何后续文本更改以下代码,您将看到标题:
self.layer.addSublayer(gradient)
改为
self.layer.insertSublayer(gradient, at: 0)
尝试更改文本颜色。添加您如何使用该按钮的代码。我已经尝试了您的代码,效果很好,我认为在使用该按钮时有一些小错误。