UITableViewCell在UITableView中出现缩进问题,已设相关属性仍未解决
解决UITableViewCell自动缩进问题
以下是几种针对性的解决办法:
配置cell的layoutMargins
仅设置tableView的layoutMargins无法覆盖cell的默认设置,需要在cellForRowAt方法或自定义cell的布局逻辑中添加:cell.layoutMargins = UIEdgeInsets.zero调整分隔线缩进
如果是系统分隔线出现缩进,需要同时设置tableView和cell的separatorInset:tableView.separatorInset = UIEdgeInsets.zero cell.separatorInset = UIEdgeInsets.zero另外,iOS 11及以上可以设置tableView的
separatorStyle为.singleLine,避免系统自动调整分隔线边距。关闭superview边距继承
若cell开启了preservesSuperviewLayoutMargins,会继承tableView的边距设置,需要手动关闭:cell.preservesSuperviewLayoutMargins = false检查自定义cell的子视图约束
如果是自定义cell内部内容缩进,确认子视图的约束是直接绑定到contentView的边缘,而非contentView的layoutMarginsGuide。例如将约束从contentView.layoutMarginsGuide.leadingAnchor改为contentView.leadingAnchor。
内容的提问来源于stack exchange,提问作者ItsAlwaysThere
相关产品推荐
相关产品推荐

