简体   繁体   English

UITableview 单元格的左右约束

[英]Left and right constraint on UITableview cell

I cannot add left and right constraints on the table as I don't want those constraints on all the cells.我不能在表格上添加左右约束,因为我不希望所有单元格上都有这些约束。 I want constraints on certain cells on left and right, however when I add the constraints programmatically, I don't see it working.我想要左右某些单元格的约束,但是当我以编程方式添加约束时,我看不到它起作用。 I see bottom and top constraints working.我看到底部和顶部约束有效。

Using auto-layout, my table view's left and right constraint is equal to main view's left and right, cell's content view is aligned to left and right of table view.使用自动布局,我的表格视图的左右约束等于主视图的左右,单元格的内容视图与表格视图的左右对齐。 Below is my code:下面是我的代码:

cell.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 6.0).isActive = true
cell.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: 6.0).isActive = true

cell.contentView.leftAnchor.constraint(equalTo: tableView.leftAnchor, constant: 9.0).isActive = true
cell.contentView.rightAnchor.constraint(equalTo: tableView.rightAnchor, constant: 9.0).isActive = true
cell.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor, constant: 9.0).isActive = true
cell.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor, constant: 9.0).isActive = true

Here is how it looks with these constraints:以下是这些约束的外观:

在此处输入图像描述

How do I add left and right constraints or space between the cell and the device edge on left and right side of the cell?如何在单元格与单元格左右两侧的设备边缘之间添加左右约束或空间?

How do I add left and right constraints or space between the cell and the device edge on left and right side of the cell?如何在单元格与单元格左右两侧的设备边缘之间添加左右约束或空间?

You don't.你没有。 The "device edge" is irrelevant and outside your purview. “设备边缘”无关紧要,不在您的权限范围内。

The cell is part of the table view and is placed where it is placed by the table view.单元格是表格视图的一部分,并放置在表格视图放置的位置。 It isn't up to you.这不取决于你。 Nor is the position of the content view up to you.内容视图的 position 也不取决于您。

Positioning the contents of the content view within the content view, however, is just fine.但是,将内容视图的内容定位在内容视图中就可以了。 Indeed, you have configurable margins for this very purpose.实际上,您可以为此目的设置可配置的边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM