简体   繁体   English

在自动布局约束中设置的值与子视图上元素的位置不匹配

[英]Values,set in auto-layout constraints doesn't match element's position on subview

There is a custom UITableViewCell subclass with xib. 有一个带有xib的自定义UITableViewCell子类。 all elements' constraints are set in IB 所有元素的约束都在IB中设置 在此处输入图片说明

Let's take a look at Test label (UILabel element). 让我们看一下Test标签(UILabel元素)。 It has 3 constraints: 它具有3个约束: 在此处输入图片说明

The trailing space to superview(cell's content view) is set to 12 . 超级视图(单元格的内容视图)的尾随空间设置为12

The leading space to superview is set to 12 超级观看的领先空间设置为12

The top space to superview is set to 5 要查看的顶部空间设置为5

But when I build and run, the real distance between left simulator's margin and label's leading side is 20 ! 但是当我构建并运行时,左侧模拟器的边距与标签的前端之间的实际距离为20 As well for top space - it is more then expected 5px 顶部空间也是如此-预期为5px

在此处输入图片说明

If I give to label long name - it gets resized and trailing space is positioned correctly -exactly to 12px, as expected. 如果我给标签加长名-它会调整大小,并且尾随空间正确放置-精确到12px,正如预期的那样。

The same situation is with 2 images below test label. 同样的情况是测试标签下方有2张图像。 While theirs leading spaces are set to 12, in reality they are distanced on 20 px from content view's left margin. 虽然它们的前导空格设置为12,但实际上它们与内容视图的左边界相距20 px。

What can be the reason of issue? 问题可能是什么原因?

By default in iOS 8 there is an 8px padding for margins (A view's margin property is UIEdgeInset with 8px padding for all four sides). 默认情况下,iOS 8中的边距填充为8px(视图的margin属性为UIEdgeInset,所有四个侧面的填充均为8px)。 When you set 12px for leading constrain the margin's 8px padding also will be added to the constrain, thats why your leading constrain is 20px. 当您为领先约束设置12px时,边距的8px填充也将添加到约束中,这就是为什么领先约束为20px的原因。

Uncheck the " Relative to Margin " option for the leading constrain to fix this issue. 取消选中“ 相对于保证金 ”选项,以解决该问题。

在此处输入图片说明

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

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