我在使用UICollectionViewCell内的自动布局。布局非常简单:只有一个UILabel。我希望UILabel占据整个宽度减去20像素的插图,并在垂直和水平方向上居中于单元格。我已经设置了相应的约束。如果在任何iOS 8设备或模拟器上运行,它都能完美地工作。然而,在某些iOS 7设备上运行时,这些约束没有效果。我尝试查看苹果文档,但它们的更改似乎与自动布局无关。
以下是XML源代码,尽管我认为它并没有什么意义:
为了使其正常工作,我需要使用编码约束和IB约束。不知道为什么!
以下是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约束。不知道为什么!