iOS 15系统下UITableView单元格出现异常间隙如何解决

核心修复方案
iOS 15 对 UITableView 的 Plain 样式间距逻辑做了修改,你之前的代码没生效是踩了三个常见坑:返回空UIView实例会触发系统默认高度、漏了估算高度配置、直接返回0高度会被系统判定为未实现代理走默认值。
直接替换对应配置代码即可:
override func viewDidLoad() { super.viewDidLoad() if #available(iOS 15.0, *) { tableView.sectionHeaderTopPadding = 0 tableView.fillerRowHeight = 0 // 关闭段头段尾的估算高度,避免系统自动分配默认高度 tableView.estimatedSectionHeaderHeight = 0 tableView.estimatedSectionFooterHeight = 0 } } override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { // 不要返回0,用极小值触发系统不渲染间距 return .leastNonzeroMagnitude } override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return .leastNonzeroMagnitude } override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { // 不要返回UIView()空实例,直接返回nil return nil } override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { return nil }
排查补充
如果改完还有间隙,逐一检查以下配置:
- 确认单元格内的所有底部约束是绑定到
contentView而非cell本身,否则自动高度计算会多出系统预留的安全边距 - 如果不需要系统默认分割线,设置
tableView.separatorStyle = .none,iOS15的默认分割线自带上下内边距 - 如果tableView放在UINavigationController里,检查
scrollView.contentInsetAdjustmentBehavior是否为.automatic,可以改成.never验证是否是导航栏导致的顶部偏移
内容的提问来源于stack exchange,提问作者Rohit Kumar
相关产品推荐
相关产品推荐

