我在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約束。不知道爲什麼!
你在iOS中7得到什麼結果呢?我沒有看到iOS 7中不適用的約束條件。 – rdelmar 2014-10-03 01:02:10
UILabel具有xib的確切高度和寬度 – user3534641 2014-10-03 01:03:17
我擁有完全相同的設置和問題。任何解決方案呢? – Ravi 2014-10-06 20:58:36