swift开发UITableViewCell的分割线与屏幕边缘对齐显示的解决方案swift
iOS开发中咱们使用UITableView控件时会发现每一个UITableviewCell的分割线左侧距离屏幕有必定的间距,这样的默认效果也很好,可是每每咱们会遇到让把cell的分割线与屏幕的左右边缘对齐就是让分割线的宽度和屏幕的宽度相同的显示效果。spa
介绍两种实现的方法:代理
第一种方法:blog
能够经过在cell中自定义一个cell底部subView的方法,使得subView的左右约束与屏幕的左右间距为0来实现,此种方法就不作重点说明。开发
第二种方法:io
经过实现UITableViewDelegate中的代理方法来实现table
optional public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
具体实现以下代码:class
//分割线从左端顶部显示(使cell的)分割线与屏幕的左右两端对齐显示 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if(cell.responds(to: #selector(setter: UITableViewCell.separatorInset))){ cell.separatorInset = .zero } if(cell.responds(to: #selector(setter: UITableViewCell.layoutMargins))){ cell.layoutMargins = .zero } }