2014-07-01 42 views
1

我正在嘗試用Autolayout構建一個應用程序。嘗試使用故事板,也編程但沒有運氣。意見不符合預期。Autolayout按預期工作

這是輸出屏幕。 (靶向iOS7)

This is the output screens.

問題:我想綠色按鈕,只是上面的紅色按鈕與10px的垂直間距。但是紅色按鈕在3.5英寸顯示屏上重疊,其在4英寸顯示屏上正確顯示。

下面是我試過的代碼。

 [self.btnGreen setTranslatesAutoresizingMaskIntoConstraints:NO]; 

     [self.viewContainer addConstraint:[NSLayoutConstraint constraintWithItem:self.btnGreen attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.viewContainer attribute:NSLayoutAttributeHeight multiplier:1.0 constant:100.0]]; 


     [self.viewContainer addConstraint:[NSLayoutConstraint constraintWithItem:self.btnGreen attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.viewContainer attribute:NSLayoutAttributeWidth multiplier:1.0 constant:100.0]]; 

我已經審閱下面的鏈接: 1)Apple guideline 2)http://matthewmorey.com/ 3)Also this one

,但沒有得到期望的輸出。

任何幫助將不勝感激。

+0

沒有必要指定第二個項目,如果你只是限制高度或寬度爲恆定。你可以這樣做:'[NSLayoutConstraint constraintWithItem:self.btnGreen attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:width];'改爲。 –

+0

它看起來好像你的綠色按鈕甚至不適合你的紅色按鈕和上面的白色框之間的差距?您希望佈局看起來如何? –

回答

2

嘗試見圖這些約束:enter image description here

+0

感謝您的快速響應。但它現在工作。我得到相同的行爲。 – iLearner

+0

通過這樣做,綠色視圖與其上方的視圖重疊。 – iLearner

+0

您應該將TopView的垂直空間約束添加到常量爲170的超視圖(btnGreenHeight + btnRedHeight +間距)。 –