Krish
Krish

Reputation: 4232

How to update and remove Constraints using Masonry?

With Auto-layouts I am adding "1" label and "1" textfield and "2" buttons on my scrollview.

Here my main requirement is when I click "Add1" button then one extra textfield must be added between "textfiled1" and buttons.

And when I click "Add2" button then another textfield must be added between "textfiled2" and buttons.

And when I click remove button both Added textfields must be removed and for this I have written some code, but this is not working.

my code:

#import "ViewController2.h"
#import "Masonry.h"

@interface ViewController2 ()

@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) UILabel *label1;
@property (nonatomic) UITextField *textField1;
@property (nonatomic) UITextField *textField2;
@property (nonatomic) UITextField *textField3;
@property (nonatomic) UIButton *leftButton;
@property (nonatomic) UIButton *rightButton;
@property (nonatomic) UIButton *removeButton;

@end

@implementation ViewController2

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];

    self.scrollView = [[UIScrollView alloc] init];

    self.label1 = [[UILabel alloc] init];
    self.textField1 = [[UITextField alloc] init];
    self.textField2 = [[UITextField alloc] init];
    self.textField3 = [[UITextField alloc] init];
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.removeButton = [UIButton buttonWithType:UIButtonTypeCustom];

    self.label1.text = @"Label 1";
    self.label1.backgroundColor = [UIColor orangeColor];

    self.textField1.placeholder = @"TextField 1";
    self.textField1.backgroundColor = [UIColor lightGrayColor];

    self.textField2.placeholder = @"TextField 2";
    self.textField2.backgroundColor = [UIColor lightGrayColor];

    self.textField3.placeholder = @"TextField 3";
    self.textField3.backgroundColor = [UIColor lightGrayColor];

    self.leftButton.backgroundColor = [UIColor greenColor];
    [self.leftButton setTitle:@"Add1" forState:UIControlStateNormal];
    [self.leftButton addTarget:self
                        action:@selector(Adding1)
              forControlEvents:UIControlEventTouchUpInside];

    self.rightButton.backgroundColor = [UIColor grayColor];
    [self.rightButton setTitle:@"Add2" forState:UIControlStateNormal];
    [self.rightButton addTarget:self
                         action:@selector(Adding2)
               forControlEvents:UIControlEventTouchUpInside];

    self.removeButton.backgroundColor = [UIColor blackColor];
    [self.removeButton setTitle:@"Remove" forState:UIControlStateNormal];
    [self.removeButton addTarget:self
                         action:@selector(Remove)
               forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.scrollView];

    [self.scrollView addSubview:self.label1];
    [self.scrollView addSubview:self.textField1];
    [self.scrollView addSubview:self.textField2];
    [self.scrollView addSubview:self.textField3];

    [self.scrollView addSubview:self.leftButton];
    [self.scrollView addSubview:self.rightButton];
    [self.scrollView addSubview:self.removeButton];

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(30, 10, 10, 10));
    }];

    [self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(@10);
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.centerX.equalTo(self.scrollView);
    }];

    [self.textField1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.label1.mas_bottom).offset(10);
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.height.equalTo(@30);
    }];

    [self.textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
    }];
    [self Removing1];

    [self.textField3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
    }];
    [self Removing2];


    //Applying Autolayouts for UIButtons:

       [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textField2.mas_bottom).offset(40);
        make.left.equalTo(@10);
        make.height.equalTo(@30);
        make.bottom.equalTo(@-10);
    }];

       [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.leftButton);
        make.right.equalTo(@-10);
        make.left.equalTo(self.leftButton.mas_right).offset(20);
        make.height.equalTo(@30);
        make.width.equalTo(self.leftButton);
        make.bottom.equalTo(@-10);
    }];

    [self.removeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftButton.mas_bottom).offset(40);
        make.top.equalTo(self.rightButton.mas_bottom).offset(40);
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.height.equalTo(@30);
        make.bottom.equalTo(@-10);
    }];
}

-(void)Adding1 {

    [self.textField2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textField1.mas_bottom).offset(10);
        make.height.mas_equalTo(30);
    }];
}

-(void)Adding2 {

    [self.textField2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textField2.mas_bottom).offset(10);
        make.height.mas_equalTo(30);
    }];
}

-(void)Remove {


}

-(void)Removing1{

    [self.textField2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textField1.mas_bottom).offset(0);
        make.height.mas_equalTo(0);
    }];
}

-(void)Removing2{

    [self.textField3 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textField2.mas_bottom).offset(0);
        make.height.mas_equalTo(0);
    }];
}

@end

Upvotes: 1

Views: 2603

Answers (1)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

You need to declare the constraints as properties.

// in public/private interface
@property (nonatomic, strong) MASConstraint *topConstraint;

To add the top constraint for your view1

// when making constraints
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
}];

and to unisntall the constraint later, you can simply call

// then later you can call
[self.topConstraint uninstall];

Upvotes: 1

Related Questions