【问题标题】:AutoLayout works in ios 8 but not in ios 7?AutoLayout 在 ios 8 中有效,但在 ios 7 中无效?
【发布时间】:2014-11-28 02:14:28
【问题描述】:

我在 UICollectionViewCell 中使用自动布局。超级简单的布局:只是一个 UILabel。我希望 UILabel 占据整个宽度减去 20 px 插图,并在单元格中垂直和水平居中。我已经设置了可以做到这一点的约束。如果我在任何 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 约束。不知道为什么!

【问题讨论】:

  • 你在 iOS 7 中得到了什么结果?我没有看到任何不应该在 iOS 7 中工作的约束。
  • UILabel 具有 xib 的确切高度和宽度
  • 我有完全相同的设置和问题。有什么解决方案吗?
  • 在 layoutsubviews 的代码中放置约束是使其工作的唯一方法。您会收到一些关于重叠约束的抱怨,但它确实有效。
  • 你可以为 iOS 7 和 8 使用不同的屏幕尺寸吗?某些尺寸类别可能未安装某些约束。

标签: ios objective-c ios7 ios8


【解决方案1】:

听起来您的 Interface Builder 约束正在使用 iOS7 不支持的“Constraint to Margin”选项。在 IB 中打开约束并检查其中是否有已选中“相对于边距”选项的项目。

请参阅What is "Constrain to margin" in Storyboard in Xcode 6What is "Constrain to margin" in Storyboard in Xcode 6 了解有关边距限制的更多详细信息。

【讨论】:

  • 我什至找不到该选项,但 xml 中没有任何内容
【解决方案2】:

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

要找到这个选项:

  1. 点击 UI 控件

  2. 转到尺寸检查器

  3. 双击任何约束并检查边距,如果有任何变化,只需将其关闭并修复约束。

【讨论】:

    猜你喜欢
    • 2015-01-28
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多