自动布局在iOS 8中可用,但在iOS 7中不可用?

5
我在使用UICollectionViewCell内的自动布局。布局非常简单:只有一个UILabel。我希望UILabel占据整个宽度减去20像素的插图,并在垂直和水平方向上居中于单元格。我已经设置了相应的约束。如果在任何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中不起作用的东西。 - rdelmar
UILabel的高度和宽度与xib完全一致。 - user3534641
1
我有完全相同的设置和问题。已经有解决方案了吗? - Ravi
将约束条件放置在layoutsubviews的代码中是使其正常工作的唯一方法。您会收到一些关于重叠约束的投诉,但它可以工作。 - user3534641
你是否在iOS 7和8中使用了不同的屏幕尺寸?你的一些约束可能没有安装到某些尺寸类别中。 - TotoroTotoro
iPhone 5s在iOS 7和iPhone 5s在iOS 8上的表现相同,iPad Mini也是如此(已尝试所有模拟器)。 - user3534641
2个回答

4

1
我甚至找不到那个选项,但是xml文件中也没有任何内容。 - user3534641

4
这种情况曾发生在我身上,您需要检查UI中的每个约束并删除相对于边距的设置,因为这种设置是在iOS 8中引入的,不支持iOS 7。别忘了在一些可能被忽略的地方(如UITableView单元格)也要检查。
要找到此选项:
1. 点击UI控件 2. 进入"Size Inspector" 3. 双击任何一个约束并检查边距设置,关闭它并根据需要修复约束。
请见下图: enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接