2014-02-19 28 views
0

因此,我在UIViewController的不同元素上使用NSLayoutConstraints,但由於某些原因,它們影響了我的按鈕。就像這樣:關於在其他元素上使用AutoLayout的UIButton自動調整功能

Screwed up buttons

這裏是我的代碼:

// Twitter button 
WelcomeButton *twitterButton = [[WelcomeButton alloc] initTwitterButtonWithFrame:CGRectMake(0, 0, 300, 60)]; 
twitterButton.center = CGPointMake(self.view.frame.size.width/2, 350); 
twitterButton.alpha = 1.0; 
twitterButton.translatesAutoresizingMaskIntoConstraints = NO; 
[twitterButton addTarget:self action:@selector(twitterSignUpButton:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:twitterButton]; 

// Facebook button 
WelcomeButton *facebookButton = [[WelcomeButton alloc] initFacebookButtonWithFrame:CGRectMake(0, 0, 300, 60)]; 
facebookButton.center = CGPointMake(self.view.frame.size.width/2, 0); 
facebookButton.alpha = 1.0; 
facebookButton.translatesAutoresizingMaskIntoConstraints = NO; 
[facebookButton addTarget:self action:@selector(facebookSignUpButton:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:facebookButton]; 

// Email Sign-Up button 
WelcomeButton *emailButton = [[WelcomeButton alloc] initEmailButtonWithFrame:CGRectMake(0, 0, 300, 60)]; 
emailButton.center = CGPointMake(self.view.frame.size.width/2, 0); 
emailButton.alpha = 1.0; 
emailButton.translatesAutoresizingMaskIntoConstraints = NO; 
[emailButton addTarget:self action:@selector(emailSignUpButton:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:emailButton]; 

// Signed-Up Message Label 
UILabel *signedUpLabel = [[UILabel alloc] init]; 
signedUpLabel.frame = CGRectMake(0, 0, 150, 25); 
signedUpLabel.center = CGPointMake(self.view.center.x - (signedUpLabel.frame.size.width/3) + 10, 0); 
signedUpLabel.textColor = [UIColor whiteColor]; 
signedUpLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:16]; 
signedUpLabel.text = @"Already Signed Up ?"; 
signedUpLabel.translatesAutoresizingMaskIntoConstraints = NO; 
[self.view addSubview:signedUpLabel]; 

// Sign-In button 
UIButton *signInButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
signInButton.frame = CGRectMake(0, 0, 100, 25); 
signInButton.center = CGPointMake(self.view.center.x + (signedUpLabel.frame.size.width/3) + 10, 0); 
signInButton.titleLabel.textColor = [UIColor whiteColor]; 
signInButton.titleLabel.font = [UIFont systemFontOfSize:16]; 
[signInButton setTitle:@"Sign In" forState:UIControlStateNormal]; 
signInButton.translatesAutoresizingMaskIntoConstraints = NO; 
signInButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; 
[self.view addSubview:signInButton]; 

這是我的自動版式代碼:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[signedUpLabel]-10-|" 
                          options:0 
                          metrics:0 
                         views:NSDictionaryOfVariableBindings(signedUpLabel)]]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[signInButton]-2-|" 
                     options:0 
                     metrics:0 
                     views:NSDictionaryOfVariableBindings(signInButton)]]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[emailButton]-200-[signedUpLabel]" 
                     options:0 
                     metrics:0 
                     views:NSDictionaryOfVariableBindings(emailButton, signedUpLabel)]]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[facebookButton]-100-[emailButton]" 
                     options:0 
                     metrics:0 
                     views:NSDictionaryOfVariableBindings(facebookButton, emailButton)]]; 

什麼錯嗎?

我想要這些在一個結構中對齊在一起,但他們爲什麼會出現這樣的?當我還沒有指定任何約束,他們通常出現...

+0

您沒有指定足夠的約束(這裏只指定'Vertical Constraint')。我在「故事板」中做這項工作......這遠遠更簡單。 –

+0

我需要'編程'! –

回答

1

您必須添加Horizontal Constraint

如果你想這樣做編程,也許你可以一起工作:https://github.com/jrturton/UIView-Autolayout

編輯: 可以添加Height Constraint像:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView(==60)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myButton)]]; 
+0

即使我添加了水平約束,「高度」也會減少。爲什麼? –

+0

對於哪個對象? –

+0

對於'UIButton' ... –

0

爲什麼不`噸您在廈門國際銀行使用自動佈局?

+0

如果我能,我會。 –