AutoLayout可以在ios 8中运行,但不能在ios 7中运行?

问题描述:

我在UICollectionViewCell中使用自动布局。超级简单的布局:只是一个UILabel。我希望UILabel占据整个宽度减去20像素插图,并且在单元格中垂直和水平居中。我已经设置了限制来做到这一点。如果我在任何ios 8设备或模拟器上运行它,它都可以完美运行。但是,当我在一些ios 7设备上运行时,约束条件不起作用。我试着翻看苹果文档,但他们的变化似乎都没有围绕自动布局。AutoLayout可以在ios 8中运行,但不能在ios 7中运行?

这里的XML源代码,但我怀疑这意味着很多:

<constraints> 
      <constraint firstItem="OIc-cg-9hO" firstAttribute="leading" secondItem="Ga6-nx-lOn" secondAttribute="leading" constant="20" id="A7U-sd-fcL"/> 
      <constraint firstAttribute="centerY" secondItem="OIc-cg-9hO" secondAttribute="centerY" id="G9e-9W-aDS"/> 
      <constraint firstAttribute="centerX" secondItem="OIc-cg-9hO" secondAttribute="centerX" id="TrB-hI-7Kw"/> 
      <constraint firstAttribute="trailing" secondItem="OIc-cg-9hO" secondAttribute="trailing" constant="20" id="yjH-nf-D9U"/> 
     </constraints> 

更多的解决方法不是一个答案,但我添加了约束代码如下:

[self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName 
                 attribute:NSLayoutAttributeWidth 
                 relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                 attribute:NSLayoutAttributeWidth 
                multiplier:1.0 
                 constant:-20.0]]; 

[self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName 
                 attribute:NSLayoutAttributeCenterX 
                 relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                 attribute:NSLayoutAttributeCenterX 
                multiplier:1.0 
                 constant:0.0]]; 

[self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName 
                 attribute:NSLayoutAttributeCenterY 
                 relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                 attribute:NSLayoutAttributeCenterY 
                multiplier:1.0 
                 constant:0.0]]; 

为了为了它的工作,我需要编码约束和IB约束。不知道为什么!

+0

你在iOS中7得到什么结果呢?我没有看到iOS 7中不适用的约束条件。 – rdelmar 2014-10-03 01:02:10

+0

UILabel具有xib的确切高度和宽度 – user3534641 2014-10-03 01:03:17

+1

我拥有完全相同的设置和问题。任何解决方案呢? – Ravi 2014-10-06 20:58:36

听起来你的Interface Builder约束使用iOS7不支持的“约束到边距”选项。打开IB中的约束条件并检查是否有任何项目的'相对于保证金'选项被选中。

请参阅What is "Constrain to margin" in Storyboard in Xcode 6What is "Constrain to margin" in Storyboard in Xcode 6了解有关约束保证金的更多详细信息。

+1

我什至不能找到该选项,但没有什么在xml – user3534641 2014-10-17 01:30:21

+0

这导致我在正确的方向。 – 2015-04-08 15:36:57

这发生在我身上,你必须检查UI中的每个约束,并删除iOS 8引入的Relative to Margin,并且不支持iOS 7,不要忘记那些你可能会忘记的地方(如UITableView单元格)。

要查找此选项:在UI控制

  • 转到尺寸检查

    1. 点击

    2. 双击任何约束,检查保证金的事,只是把它关掉如果有任何改变,修复你的约束。

    enter image description here