2015-04-06 109 views
0

我有一個要求,其中,我試圖在水平放置沒有任何間隙的視圖底部設置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]]; 

required Actual

請幫我解決這個問題

編輯:在iOS系統7,看到屏幕截圖

enter image description here

謝謝, Vinod。

+0

這是在界面生成器中做一個選項嗎?我知道如何去做... – user2320861

+0

否則,我會將每個按鈕的寬度設置爲W,其中W是度量值 - 包含按鈕的視圖的寬度的1/3。指標:@ {@「W」:1.0/3.0 * buttonsSuperView.width} – user2320861

回答

2

我試過你的代碼和約束似乎很好地工作。問題可能在其他地方。

這是我試過的代碼,創建的所有按鈕編程:

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。如果您在故事板中創建它們,則需要刪除在其中添加的隱式約束。

讓我知道這是怎麼回事,如果你需要更多的幫助。

+0

嗨Catalina感謝您的幫助。它適用於iOS 8,適用於各種屏幕尺寸,但不適用於iOS 7。我在這裏丟失了什麼 – Atom

+0

看看我的水平約束,空間在按鈕之間明確設置爲0。您是否對您的代碼進行了更改?這使它也適用於我在iOS7上,我只是試過它 –

+0

我用你的代碼,並在ios 7模擬器中運行,但我仍然得到那個屏幕,我連接 – Atom

相關問題