adjustsFontForContentSizeCategory导致UIButton UI测试异常问题咨询
问题原因
该问题是UIButton状态渲染逻辑、动态类型配置时序和UI测试快照采集机制共同作用的结果,核心触发点如下:
titleLabel?.adjustsFontForContentSizeCategory = true会给按钮内置的titleLabel注册动态类型变更监听,触发UIButton内部的额外状态刷新逻辑。UIButton的titleLabel是私有托管视图,直接修改它的属性会触发非预期的视图重绘。- 你的调用时序是先设置titleColor、后设置按钮文本:正常运行时UIKit会合并同一渲染帧的所有修改,不会出现异常;但UI测试会优先同步采集当前视图的状态快照,开启动态类型调整触发的异步状态刷新,会覆盖你之前设置的normal状态下的titleColor配置,导致新设置的文本使用系统默认颜色或按钮tintColor,出现颜色失效的现象。
- 注释掉动态类型配置后,不会触发额外的异步状态刷新,你设置的样式和文本会按调用顺序正常生效,UI测试可以正常采集到正确样式。
修复方案
方案1:调整调用顺序
先设置按钮文本,再调用样式配置方法,确保样式配置最后生效:
button1.setTitle("Some title", for: .normal) button1.applyStyle(.primary)
方案2:调整扩展内的代码顺序
把动态类型配置的代码放到所有样式配置的最后,避免提前触发的重绘覆盖后续样式:
extension UIButton { func applyStyle(_ type: CustomButtonType) { 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) } // 移到所有样式配置之后 titleLabel?.adjustsFontForContentSizeCategory = true } }
方案3:强制触发布局刷新
设置完所有属性后强制同步布局,确保UI测试能采集到最新样式:
button1.applyStyle(.primary) button1.setTitle("Some title", for: .normal) button1.setNeedsLayout() button1.layoutIfNeeded()
内容的提问来源于stack exchange,提问作者Async-
相关产品推荐
相关产品推荐

