我知道如何设置标题文本并对其进行样式设置,但在我的一生中,我找不到任何解释如何调整文本缩进的内容。这是我尝试过的:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
let header = view as! UITableViewHeaderFooterView
header.textLabel?.translatesAutoresizingMaskIntoConstraints = false
header.textLabel?.leftAnchor.constraint(equalTo: header.leftAnchor, constant: 15).isActive = true
}
上面的代码(以及我尝试过的每个变体)都会导致错误:
Unable to activate constraints because they have no common ancestor.
这让我对我的计算机说,“如果 header.textLabel
让我到达 UILabel
,并且 header
是确实是一个 UITableViewHeaderFooterView
,我想你应该知道一个是另一个的祖先!”
最佳答案
您可以通过首先为整个 TableView
设置 separatorInset
来实现此目的,如下所示:
let headerInset: CGFloat = 50
tableView.separatorInset = UIEdgeInsets.init(top: 0, left: headerInset, bottom: 0, right: 0)
然后,如果您想要将表格单元格定位到与 TableView
的边缘对齐,您可以在 forRowAt indexPath
方法中执行此操作:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.separatorInset = UIEdgeInsets.zero
}
关于ios - 如何在 Swift 中调整 TableView 标题的缩进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43132547/