我正在嘗試用Autolayout構建一個應用程序。嘗試使用故事板,也編程但沒有運氣。意見不符合預期。Autolayout按預期工作
這是輸出屏幕。 (靶向iOS7)
問題:我想綠色按鈕,只是上面的紅色按鈕與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
,但沒有得到期望的輸出。
任何幫助將不勝感激。
沒有必要指定第二個項目,如果你只是限制高度或寬度爲恆定。你可以這樣做:'[NSLayoutConstraint constraintWithItem:self.btnGreen attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:width];'改爲。 –
它看起來好像你的綠色按鈕甚至不適合你的紅色按鈕和上面的白色框之間的差距?您希望佈局看起來如何? –