2012-10-25 116 views
0

非常簡單(我希望)自動佈局問題,我對我的iMac敲了敲頭。UIStoryboard自動佈局問題與約束

我有這樣的肖像

enter image description here

這是景觀發生了什麼。

enter image description here

所有我想要的是標籤攤開就像他們,如果你不使用自動佈局。 我添加了什麼約束來均勻地將它們分開?

+0

[Autolayout Even Spacing]的可能重複(http://stackoverflow.com/questions/13075415/autolayout-even-spacing) – jrturton

+0

如果您不想或需要自動佈局,請關閉它 - 文件檢查器在故事板,取消選中使用自動佈局。 – jrturton

+0

我可以爲故事板中的一個View Controller做到這一點嗎?我不想關閉它,我想學習如何解決這個問題。 –

回答

2

有一個解決方案,比@ sust86的有些簡單,也比較靈活:

  1. 介紹空作爲標籤之間的彈簧的視圖。
  2. 以恆定的距離約束連接所有相鄰的視圖。
  3. 將其中一個新的彈簧視圖與所有其他具有相同寬度約束的彈簧視圖相連接。
  4. 刪除任何不需要的約束。

對我的約束則類似於此:

enter image description here

該配置具有不需要解決任何間距的優勢,當你的標籤的寬度(在我的情況按鈕)改變。

1

在iOS 6中使用新的約束是棘手的。 ray的網站上有一個很好的2部分教程,與iOS 6中的自動佈局相關。雖然它解釋瞭如何在自動佈局中錨定圖像保持器,但原理幫助我理解了整個自動佈局。希望這也能幫助你。 Herer是鏈接: http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2 阿德里安

+0

這是一個很好的教程,但我沒有看到任何與問題直接相關的內容。 –

1

試試這個:

#define moneyLabelWidth 20 
@property (nonatomic, strong) UILabel* moneyOneLabel; 
@property (nonatomic, strong) UILabel* moneyTwoLabel; 
@property (nonatomic, strong) UILabel* moneyThreeLabel; 
@property (nonatomic, strong) UILabel* moneyFourLabel; 
@property (nonatomic, strong) UILabel* moneyFiveLabel; 
@property (nonatomic, strong) UILabel* moneySixLabel; 
... 
[self.view addSubview:moneyOneLabe]; 
[self.view addSubview:moneyTwoLabe]; 

moneyOneLabel.translatesAutoresizingMaskIntoConstraints = NO; 
moneyTwoLabel.translatesAutoresizingMaskIntoConstraints = NO 
etc 
... 


[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_moneyOneLabel(moneyLabelWidth)]-[_moneyTwoLabel(moneyLabelWidth)]-[_moneyThreeLabel(moneyLabelWidth)]-[_moneyFourLabel(moneyLabelWidth)]-[_moneyFiveLabel(moneyLabelWidth)]-[_moneySixLabel(moneyLabelWidth)]-|" 
                    options:0 
                    metrics:@{@"moneyLabelWidth":@(moneyLabelWidth)} 
                    views:NSDictionaryOfVariableBindings(_moneyOneLabel, _moneyTwoLabel, _moneyThreeLabel, _moneyFourLabel, _moneyFiveLabel, moneySixLabel)]]; 
/*add vertical constraints*/ 

以上constrainsWithVisualFormat基本上都是從左邊說右邊緣horizo​​ntaly繪製6級金錢的標籤,所有與20的寬度,它們之間寬度可變。我沒有運行這個代碼,但我認爲它會起作用(或者至少是接近)。

1

最簡單的方法是在每個視圖之間使用2約束。一個與大於或等於和一個與小於或等於。 最小尺寸(大於或等於)應該是縱向模式下的間距。 最大尺寸(Lesser Then或Equal)應該是橫向模式下的間距。

這是相同的解決方案,因爲我提供的這個問題:

Using Auto Layout to change spaces equally on resizing