gpt4 book ai didi

ios - 如何在 Swift 中调整 TableView 标题的缩进?

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:04 25 4
gpt4 key购买 nike

我知道如何设置标题文本并为其设置样式,但就我的生活而言,我找不到任何解释如何调整文本缩进的内容。这是我尝试过的:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com