Krish
Krish

Reputation: 4232

How to set Auto-layouts in Ios when we tapped on button?

Hi I am beginner for auto-layouts and in my project I am using auto-layouts and I am adding one label and two textviews and two buttons on main scrollview and so for everything is ok.

Here my main requirement is when I click on button1 then "textview2" must be added between label and textview1 as like in the below image. Ok that's fine using my below code.

But here my main requirement is when I clicked button2 textview2 is removed between label and textview1 and the design seems like below image for this I have written some code in "buttonClicked2", but textview2 is not removing what did I do here wrong?

My code:

@interface ViewController7 ()
{
    UIScrollView * scrollView;
    UILabel * label1;
    AutoGrowingTextView * textview1;
    AutoGrowingTextView * textview2;
    UIButton * button1;
    UIButton * button2;
    NSDictionary * views;
    NSArray * labelConstraint;
    NSArray * constraints1;
    NSArray * horizontalconstraints;
    NSArray * verticalconstraints;
}

@end

@implementation ViewController7

- (void)viewDidLoad {

    [super viewDidLoad];

    scrollView = [[UIScrollView alloc]init];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    scrollView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:scrollView];

    label1 = [[UILabel alloc] init];
    label1.text = @"MD(Medician)";
    label1.backgroundColor = [UIColor orangeColor];
    label1.textAlignment = NSTextAlignmentCenter;
    label1.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:label1];

    textview1 = [[AutoGrowingTextView alloc] init];
    textview1.backgroundColor = [UIColor greenColor];
    textview1.textColor = [UIColor whiteColor];
    textview1.text = @"While de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figure";
    textview1.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:textview1];

    textview2 = [[AutoGrowingTextView alloc] init];
    textview2.backgroundColor = [UIColor blueColor];
    textview2.textColor = [UIColor whiteColor];
    textview2.text = @"While de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to postWhile de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to postWhile de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to postWhile de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to postWhile de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to post";
    textview2.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:textview2];

    button1 = [[UIButton alloc] init];
    [button1 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
    [button1 setTitle:@"Login" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor orangeColor];
    button1.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:button1];

    button2 = [[UIButton alloc] init];
    [button2 addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside];
    [button2 setTitle:@"Reset" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor blackColor];
    button2.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:button2];

    [self applyingConstraints];
}

-(void)applyingConstraints
{
    //Applying autolayouts
    views = NSDictionaryOfVariableBindings(scrollView,label1,textview1,button1,button2,textview2);

    NSArray * horizontalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollView]-0-|" options:0 metrics:nil views:views];

    NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[scrollView]-0-|"options:0 metrics:nil views:views];

    [self.view addConstraints:horizontalConstraint];
    [self.view addConstraints:verticalConstraint];

    //Applying autolayouts for UIlabel
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:label1
                                                           attribute:NSLayoutAttributeCenterX
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:scrollView
                                                           attribute:NSLayoutAttributeCenterX
                                                          multiplier:1
                                                            constant:0]];

    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[label1]-10-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:views]];

    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[textview1]-10-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:views]];

    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[button1(50)]-10-[button2]-10-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:views]];

    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[textview1]-10-[button2(30)]-20-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:views]];

    constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview1]-10-[button1(30)]-20-|"
                                                          options:0
                                                          metrics:nil
                                                            views:views];
    [scrollView addConstraints:constraints1];
}

- (void)buttonClicked1 :(id)sender{

    [scrollView removeConstraints:constraints1];

    horizontalconstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[textview2]-10-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:views];
    [scrollView addConstraints:horizontalConstraints];

    verticalconstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview2]-10-[textview1]-30-[button1(30)]-20-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:views];

    [scrollView addConstraints:verticalconstraints];

    [scrollView setNeedsLayout];
}

- (void)buttonClicked2 :(id)sender{

    [scrollView removeConstraints:horizontalConstraints];
    [scrollView removeConstraints:verticalconstraints];

    constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview1]-10-[button1(30)]-20-|"
                                                          options:0
                                                          metrics:nil
                                                            views:views];
    [scrollView addConstraints:constraints1];

    [scrollView setNeedsLayout];
}

@end

enter image description here

Upvotes: 0

Views: 63

Answers (2)

user3480295
user3480295

Reputation: 1048

Updated

change

[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[textview1]-10-[button2(30)]-20-|"
                                                                           options:0
                                                                           metrics:nil
                                                                             views:views]];

constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview1]-10-[button1(30)]-20-|"
                                                              options:0
                                                              metrics:nil
                                                                views:views];

to

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual
                                                          toItem:button2 attribute:NSLayoutAttributeCenterY multiplier:1.f constant:0]];

constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview2(0)]-[textview1]-10-[button1(30)]-20-|"
                                                       options:0
                                                       metrics:nil
                                                         views:views];

and remove

constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview1]-10-[button1(30)]-20-|"
                                                           options:0
                                                           metrics:nil
                                                             views:views];

in method buttonClicked2:. It' unnecessary.

BTW, try to debug these AutoLayout issues with log in Xcode. It' helpful.

Upvotes: 1

Mark Dominick Flores
Mark Dominick Flores

Reputation: 313

The scrollview hasn't received yet the trigger to apply the constraints to the view. Try adding this line to update the constraints

[scrollView setNeedsLayout];

Upvotes: 0

Related Questions