Skip to content

Constraints 0

Azmal Tech edited this page Jan 5, 2017 · 1 revision

@implementation ViewController { UIButton *secondButton; }

secondButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [secondButton setTitle:@"Second" forState:UIControlStateNormal]; [secondButton sizeToFit]; secondButton.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:secondButton];

constraint = [NSLayoutConstraint constraintWithItem:secondButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-40.f];

[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:secondButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];

[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:secondButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:200.0f];

[self.view addConstraint:constraint];

@implementation ViewController { UIButton *firstButton; }

  • (void)viewDidLoad { [super viewDidLoad];

    firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [firstButton setTitle:@"First" forState:UIControlStateNormal];
    [firstButton sizeToFit];
    firstButton.translatesAutoresizingMaskIntoConstraints = NO;
      [self.view addSubview:firstButton];
    }

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:firstButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:20.f];

[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:firstButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.f];

[self.view addConstraint:constraint];

Clone this wiki locally