我正在情节提要中定义自定义 UITableViewCell
.我还使用了一些内置的标准样式单元格.我需要设置约束,以便我的自定义单元格与平台定义的单元格匹配(它们的标题在左侧正确对齐).
I'm defining custom UITableViewCell
s in a storyboard. I'm also using some of the built in standard style cells. I need to set up constraints so that my custom cells match with the platform defined ones (have their titles correctly aligned at the left).
我注意到单元格的左对齐在 iOS 版本和运行它的设备之间发生了变化.似乎左对齐与默认单元格separatorInset
相同.
I've noticed that the left alignment of cells changes between iOS versions and the device it's running on. It seems like the left alignment is the same as the default cell separatorInset
.
这里是 UITableViewCell
的 separatorInset
用于一些设备和 iOS 版本:
Here is UITableViewCell
s separatorInset
for a few devices and iOS versions:
其他设备(iPhone 6+、iPad Mini)可能有所不同——我没有彻底检查过.
Other devices (iPhone 6+, iPad Mini) could be different – I've not exhaustively checked.
您将如何确保自定义单元格和内置单元格的标签左对齐相同?
How would you go about ensuring that the custom cells and built in cells have the same left alignment of their labels?
是否有一种合理的方式来获取这些插入默认值并将它们用于在情节提要中设置的自动布局约束中?在代码中呢?
Is there a sensible way to obtain these inset defaults and use them in auto layout constraints that are set up in a storyboard? How about in code?
我遇到了同样的问题,但另一个答案对我没有帮助,因为我在 UITableViewController 中使用了静态单元格.经过几次反复试验,我终于找到了一种无需一行代码即可解决 iPhone 6+ 上 separatorInset
问题的方法.
I was having the same issue but the other answer couldn't help me as I was using static cells in UITableViewController. After a few trial-and-errors, I finally found a way to solve the separatorInset
issue on iPhone 6+ without a single line of code.
我只是将 UILabel 的左边缘与左边距的值 0 [内容视图(当前距离 = 0)] 对齐.并更改 UILabel 的 X 位置以获取 0.(对于我的情况,在 Interface Builder 中,边距为 7,UILabel 的 X = 8).对齐在 iPhone 4S、5S、6 和 6+ 上运行良好.
I simply align the left edge of the UILabel with value of 0 [Content View (current distance = 0)] from the left margin. And also change the UILabel's X position to get 0. (For my case, in Interface Builder, margin was 7, UILabel's X = 8). The alignment works well on iPhone 4S, 5S, 6 and 6+.
这在 XCode 7 中发生了变化.附加步骤:将各个单元格的 TableViewCell
及其 contentView
的Preserve Superview Margins"设置为 TRUE 或在 IB -> Size Inspector 中检查.参考@tebs1200 的回答.
This changed in XCode 7.
Additional step: Set "Preserve Superview Margins" of the respective cell's TableViewCell
and its contentView
to TRUE or check it in IB -> Size Inspector. Refer to @tebs1200's answer.
希望这会有所帮助.
这篇关于匹配自定义 & 的左对齐标准 UITableViewCell 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!