自定义 UIButton class 不显示标题
Custom UIButton class does not show title
我创建了一个自定义 UIButton
class。但是标题没有显示。我只得到背景颜色和圆角。但是标题不可见。
另外,当我在模拟器中 运行 时,iOS 13+,它的标题显示为白色。但是当我在我的设备中 运行 即 iOS 12.4 时,它不起作用。
这是我的代码:
public class CornerButton : UIButton {
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
self.layoutIfNeeded()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
self.layoutIfNeeded()
}
func setup() {
self.titleLabel?.font = UIFont(name: "Poppins-SemiBold", size: 12)
self.backgroundColor = UIColor(named: "BarColor")
self.setTitleColor(.white, for: .normal) // this line is not working
self.clipsToBounds = true
}
public override func layoutSubviews() {
self.roundCorners(corners: [.topRight,.bottomLeft], radius: 10)
}
}
您需要致电super.layoutSubviews()
public override func layoutSubviews() {
super.layoutSubviews()
self.roundCorners(corners: [.topRight,.bottomLeft], radius: 10)
}
顺便说一句,这里你不需要 layoutSubviews
,因为当你需要制作一些取决于视图的实际测量边界的东西时使用它,并且当你设置静态半径时你可以省略它
我创建了一个自定义 UIButton
class。但是标题没有显示。我只得到背景颜色和圆角。但是标题不可见。
另外,当我在模拟器中 运行 时,iOS 13+,它的标题显示为白色。但是当我在我的设备中 运行 即 iOS 12.4 时,它不起作用。
这是我的代码:
public class CornerButton : UIButton {
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
self.layoutIfNeeded()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
self.layoutIfNeeded()
}
func setup() {
self.titleLabel?.font = UIFont(name: "Poppins-SemiBold", size: 12)
self.backgroundColor = UIColor(named: "BarColor")
self.setTitleColor(.white, for: .normal) // this line is not working
self.clipsToBounds = true
}
public override func layoutSubviews() {
self.roundCorners(corners: [.topRight,.bottomLeft], radius: 10)
}
}
您需要致电super.layoutSubviews()
public override func layoutSubviews() {
super.layoutSubviews()
self.roundCorners(corners: [.topRight,.bottomLeft], radius: 10)
}
顺便说一句,这里你不需要 layoutSubviews
,因为当你需要制作一些取决于视图的实际测量边界的东西时使用它,并且当你设置静态半径时你可以省略它