在我的 UITableView
中,我想要一个“居中”分隔符效果,其中分隔符从左侧缩小 30pt,从右侧缩小 30pt。
我已经设法通过 Interface Builder 设置 TableView 本身的“Custom Insets”属性来实现这一点,但我无法通过代码重现此行为(我必须这样做)。
特别是这段代码:
self.tableView.separatorColor = .green
self.tableView.separatorStyle = .singleLine
self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
还有这个:
@objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "recent_cell") as! TPExpandableTableViewCell
//setting cell insets
cell.separatorInset = separatorInset
cell.item = items[indexPath.row]
return cell
}
我在 iPhone 6S 模拟器 上获得了以下输出:
似乎分隔符内容 View 缩小了,但分隔符背景 View 没有缩小。我还尝试删除设置单元格的 separatorInset 的行,结果是插入等于 UIEdgeInset.zero
我可以确认绿色on下的白线是separator-related view,因为如果我把separatorStyle改成.none
,它就消失了
有什么帮助吗?
最佳答案
基本上,第一段代码对于设置分隔符插入、颜色和样式是正确的:
self.tableView.separatorColor = .green
self.tableView.separatorStyle = .singleLine
self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
单元格的 separatorInset 是单元格的内容。所以我对你在这里真正想要实现的目标感到困惑。从你的最后一句话开始,你的内容被缩小了,这是由“cell.separatorInset = separatorInset”引起的,你不希望这样。
所以我建议你删除这行代码:
cell.separatorInset = separatorInset
关于ios - 在 UITableViewCell 上设置自定义分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40744389/