Jus*_*ely 6 uitableview ios uiedgeinsets swift
我有一个UITableViewCell扩展到所有边缘的内容。我在需要不同插图的多个表格中使用此单元格。(即,一个表格将从左侧插入 8 个点,另一个将它插入 20 个点。我想缩进父UITableView代码中的单元格(也许cellForRow?)而不缩进整个表格。
这显然是为整个表格设置的,但我想根据需要逐个单元地进行设置:
tableView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: -20)
Run Code Online (Sandbox Code Playgroud)
这没有效果:
tableViewCell.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: -20)
Run Code Online (Sandbox Code Playgroud)
这也不起作用(即使它在单元格本身上):
override func layoutMarginsDidChange() {
super.layoutMarginsDidChange()
self.layoutMargins = UIEdgeInsets(top: 0, left: 20 bottom: 0, right: -20)
}
Run Code Online (Sandbox Code Playgroud)
确保限制 xib/storyboard 中的边距。然后在 上进行以下设置UITableViewCell:
var topInset: CGFloat = 0
var leftInset: CGFloat = 0
var bottomInset: CGFloat = 0
var rightInset: CGFloat = 0
override func layoutMarginsDidChange() {
super.layoutMarginsDidChange()
self.layoutMargins = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
}
Run Code Online (Sandbox Code Playgroud)
然后在 tableView 中cellForRowAt,使用以下内容:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let browseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "BrowseTableViewCell", for: indexPath) as! BrowseTableViewCell
browseTableViewCell.leftInset = 20
browseTableViewCell.rightInset = 20
// Top and bottom insets are not needed. They will default to 0.
return browseTableViewCell
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8007 次 |
| 最近记录: |