2014-10-03 70 views
5

我在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約束。不知道爲什麼!

+0

你在iOS中7得到什麼結果呢?我沒有看到iOS 7中不適用的約束條件。 – rdelmar 2014-10-03 01:02:10

+0

UILabel具有xib的確切高度和寬度 – user3534641 2014-10-03 01:03:17

+1

我擁有完全相同的設置和問題。任何解決方案呢? – Ravi 2014-10-06 20:58:36

回答

5

聽起來你的Interface Builder約束使用iOS7不支持的「約束到邊距」選項。打開IB中的約束條件並檢查是否有任何項目的'相對於保證金'選項被選中。

請參閱What is "Constrain to margin" in Storyboard in Xcode 6What is "Constrain to margin" in Storyboard in Xcode 6瞭解有關約束保證金的更多詳細信息。

+1

我什至不能找到該選項,但沒有什麼在xml – user3534641 2014-10-17 01:30:21

+0

這導致我在正確的方向。 – 2015-04-08 15:36:57

4

這發生在我身上,你必須檢查UI中的每個約束,並刪除iOS 8引入的Relative to Margin,並且不支持iOS 7,不要忘記那些你可能會忘記的地方(如UITableView單元格)。

要查找此選項:在UI控制

  • 轉到尺寸檢查

    1. 點擊

    2. 雙擊任何約束,檢查保證金的事,只是把它關掉如果有任何改變,修復你的約束。

    enter image description here

  • 相關問題