【发布时间】: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