2014-07-22 45 views
0

我總是在代碼中創建我的視圖,因此從未使用過自動佈局(直到現在)。不使用nib的原因是因爲我更喜歡代碼,通過GitHub等進行維護更簡單,但足夠了。Autolayout無法同時滿足約束條件

我有一種情況,我在屏幕上放置了三個標籤,彼此相鄰。像這樣:|Label1| - some space - |Label2| - some space - |Label3|。有些空間是讓我們說15分。現在我想完成的是,當我更改標籤的文本時,我會打電話給他們sizeToFit,並希望他們會保持彼此相距10pt。我知道我可以通過一些數學來解決這個問題,但我認爲自動佈局會更容易,更易於理解,並且我將在此學習一些新內容。因此,我所做的是:

  • 的Alloc &初始化所有的標籤
  • 給每個標籤的框架(和設置字體,文本顏色等)
  • 添加標籤作爲一個子視圖

然後我試圖在它們之間建立的約束,像這樣:

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[label1]-15-[label2]-15-[label3]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2, label3)]; 
[self addConstraints:constraints]; //Im subclassing an UIView that's why self 

,但我得到一個錯誤說:

Unable to simultaneously satisfy constraints. 
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x14c14110 H:[UILabel:0x14c12ab0]-(15)-[UILabel:0x14c12f80]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x14c08b00 h=--& v=--& UILabel:0x14c12ab0.midX == + 195>", 
    "<NSAutoresizingMaskLayoutConstraint:0x14c0dd90 h=--& v=--& H:[UILabel:0x14c12ab0(40)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x14c1c170 h=--& v=--& UILabel:0x14c12f80.midX == + 237.5>", 
    "<NSAutoresizingMaskLayoutConstraint:0x14c12f00 h=--& v=--& H:[UILabel:0x14c12f80(40)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14c14110 H:[UILabel:0x14c12ab0]-(15)-[UILabel:0x14c12f80]> 

即使我試圖在標籤2和3上執行此操作,也會發生同樣的情況。我不完全瞭解NSLog的說法。

任何幫助將不勝感激。此外,如果你有什麼好的鏈接來解碼那些h=--& v=--& UILabel:0x14c12f80.midX == + 237.5事情會很棒。

回答

2

h=v=是被轉換爲約束的自動修復掩碼的簽名。您是否忘記在您的某個視圖上設置translatesAutoResizingMasksToConstraints = NO

相關問題