簡體   English   中英

如何使用自動布局水平設置三個按鈕而無間隙

[英]How to set three buttons horizontally without gap using autolayout

我有一個要求,我嘗試在視圖底部將3個按鈕設置為水平放置,沒有任何間隙。 我已附上我需要顯示的屏幕截圖,以及另一個顯示當前顯示方式的屏幕截圖。

我正在以編程方式使用以下約束來設置此約束

NSDictionary *views = NSDictionaryOfVariableBindings(btnCreateAccount,btnForgotuserid,btnForgotPassword);

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:btnCreateAccount attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[btnCreateAccount][btnForgotuserid(==btnCreateAccount)][btnForgotPassword(==btnCreateAccount)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:views]];

需要實際

請幫我解決這個問題

編輯:在iOS 7中,請看屏幕截圖

在此處輸入圖片說明

謝謝,維諾德。

我已經嘗試過您的代碼,並且約束似乎工作正常。 問題可能出在其他地方。

這是我嘗試過的代碼,以編程方式創建了所有按鈕:

UIButton *b1 = [[UIButton alloc] init];
UIButton *b2 = [[UIButton alloc] init];
UIButton *b3 = [[UIButton alloc] init];

for (UIButton *b in @[b1, b2, b3]) {
    [b setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:b];
    [b.layer setBorderWidth:1];
}
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:b1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[b1]-0-[b2(==b1)]-0-[b3(==b1)]-0-|" options:NSLayoutFormatAlignAllBottom metrics:nil views:@{ @"b1":b1, @"b2":b2, @"b3":b3 }]];

確保您在按鈕上調用setTranslatesAutoresizingMaskIntoConstraints:NO 如果在情節提要中創建它們,則需要刪除在其中添加的隱式約束。

讓我知道如何進行以及是否需要更多幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM