Reputation: 319
So, I'm using NSLayoutConstraints
on different elements of my UIViewController
but for some reason they're affecting my buttons. Like this :
Here's my code :
// 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];
And here's my AutoLayout code :
[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)]];
What's going wrong here ?
I want these to be aligned together in a structure but why are they appearing like this ? When I haven't specified any constraints, they're appearing normally...
Upvotes: 0
Views: 279
Reputation: 1274
You have to add Horizontal Constraint
If you want do it programmatically, maybe you could work with : https://github.com/jrturton/UIView-Autolayout
Edit :
you can add an Height Constraint
like :
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView(==60)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myButton)]];
Upvotes: 1