iOS 15下UITableViewCell accessoryView重加载后frame变更问题求助
核心原因:iOS 15 对 UITableViewCell 的布局逻辑做了调整,新增的自动配置机制会默认覆盖开发者手动设置的 accessoryView frame,所以iOS14及之前版本正常的frame修改逻辑在iOS15上会出现错乱。
适配方案
以下方案均不需要写死x、y轴数值:
方案1:使用Auto Layout替代手动frame设置(推荐)
完全通过约束确定控件尺寸,适配所有系统版本,不会被系统布局覆盖:
let notificationLabel = UILabel() notificationLabel.translatesAutoresizingMaskIntoConstraints = false // 此处可添加label的其他配置,比如字体、文字颜色、对齐方式等 let containerView = UIView() containerView.translatesAutoresizingMaskIntoConstraints = false containerView.clipsToBounds = true containerView.backgroundColor = .clear containerView.addSubview(notificationLabel) // 激活约束 NSLayoutConstraint.activate([ // label完全贴死容器的四边 notificationLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor), notificationLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor), notificationLabel.topAnchor.constraint(equalTo: containerView.topAnchor), notificationLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor), // 可选:设置容器的最小宽高,兼容空文本场景 containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: 30), containerView.heightAnchor.constraint(equalToConstant: 30) ]) cell.accessoryView = containerView
后续需要根据文本调整容器宽度时,不需要修改任何frame,直接给notificationLabel赋值即可,约束会自动撑开容器宽度,系统布局时会自动适配accessoryView的位置,不会出现错乱。
方案2:兼容原有frame逻辑的轻量适配
如果不想改动原有frame计算逻辑,可以通过关闭系统自动配置开关来避免frame被覆盖:
在cellForRowAtIndexPath方法中修改完containerView的frame后,添加如下代码:
if #available(iOS 15.0, *) { // 关闭cell的自动配置更新,避免系统覆盖自定义布局 cell.automaticallyUpdatesContentConfiguration = false } // 主动触发布局刷新 cell.setNeedsLayout() cell.layoutIfNeeded()
注意事项
- 不要在
cellForRowAtIndexPath中重复创建containerView和notificationLabel,建议将控件封装到自定义UITableViewCell中,复用控件避免重复创建带来的性能问题和布局异常。
内容的提问来源于stack exchange,提问作者Hassy
相关产品推荐
相关产品推荐

