user5026700
user5026700

Reputation:

How to add UIbutton on UIview programmatically in ios using autolayouts

i want to add uibutton on UIview programmatically using autolayouts and tried some code but it's not working please help me some one

//Adding UIview using autolayouts

UIView * myView;
myView = [UIView new];
    myView.translatesAutoresizingMaskIntoConstraints = NO;
    myView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
    [self.view addSubview:myView];

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

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

    constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
    [self.view addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
    [self.view addConstraint:constraint];

Adding button on uiview using autolayouts:-

mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [mainButton setTitle:@"MainButton" forState:UIControlStateNormal];
    [mainButton sizeToFit];
    mainButton.backgroundColor = [UIColor blackColor];
    mainButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:mainButton];

 NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myview attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.f];
    [self.view addConstraint:constraint];

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

constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:20.f];
    [self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:20.f];
    [self.view addConstraint:constraint];

But button is not adding on myview please help me some one

Upvotes: 2

Views: 2331

Answers (2)

Ashish Kakkad
Ashish Kakkad

Reputation: 23892

This is working :

UIView *myView = [UIView new];
myView.translatesAutoresizingMaskIntoConstraints = NO;
myView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view addSubview:myView];

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

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

constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:140.0f];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];

UIButton *mainButton = [UIButton new];

mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mainButton setTitle:@"MainButton" forState:UIControlStateNormal];
[mainButton sizeToFit];
mainButton.backgroundColor = [UIColor blackColor];
mainButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:mainButton];

NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.f];
[self.view addConstraint:constraint1];

constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.f];
[self.view addConstraint:constraint1];

constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:20.f];
[self.view addConstraint:constraint1];

constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:20.f];
[self.view addConstraint:constraint1];

enter image description here

Upvotes: 2

Prajeet Shrestha
Prajeet Shrestha

Reputation: 8108

You have not defined height constraint for button, you have define width constraint twice.

Upvotes: 0

Related Questions