【发布时间】:2021-08-07 10:54:59
【问题描述】:
我有一个按钮,它配置了如下所示的扩展名:
extension UIButton {
func applyStyle(_ type: CustomButtonType) {
titleLabel?.adjustsFontForContentSizeCategory = true
switch type {
case .primary:
backgroundColor = K.Colors.callToActionPrimary
layer.cornerRadius = 10
setTitleColor(UIColor.white, for: .normal)
case .secondary:
backgroundColor = UIColor.systemBackground
layer.cornerRadius = 10
layer.borderColor = K.Colors.callToActionPrimaryBorder?.cgColor
layer.borderWidth = 1
setTitleColor(K.Colors.callToActionPrimary, for: .normal)
case .tertiary:
backgroundColor = UIColor.systemBackground
layer.cornerRadius = 10
setTitleColor(K.Colors.callToActionPrimary, for: .normal)
}
}
}
按钮设置顺序如下:
button1.applyStyle(.primary)
button1.setTitle("Some title", for: .normal)
虽然这适用于正常流程,但一旦我添加 UI 测试,setTitleColor() 似乎已损坏(或稍后执行)。
但是,如果我注释掉 titleLabel?.adjustsFontForContentSizeCategory = true 行,它可以在 UI 测试中使用。
为什么会坏掉?
【问题讨论】:
-
显示按钮本身的代码。你如何设置标题?似乎问题在于调用函数的顺序。
-
@MojtabaHosseini 我通过编辑更新了问题,我包括了调用顺序请检查