How to vertically center two views and divide the width compared to the super view?

I am trying to align two Button in a View (appears in cyan). They are aligned vertically in the View . When I try to level them horizontally, their heights change. I want their width to be the same according to the width of the superview, unlike the smaller Next button. Can this be done only with VFL ?

2 Buttons to in a container view

Here is the code:

 [self prepareButton:saveButton label:@"SAVE"]; [buttonContainerView addConstraint:[NSLayoutConstraint constraintWithItem:saveButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:buttonContainerView attribute:NSLayoutAttributeCenterY multiplier:1.00f constant:0]]; [saveButton layoutIfNeeded]; [self prepareButton:nextButton label:@"NEXT"]; [buttonContainerView addConstraint:[NSLayoutConstraint constraintWithItem:nextButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:buttonContainerView attribute:NSLayoutAttributeCenterY multiplier:1.00f constant:0]]; [nextButton layoutIfNeeded]; [buttonContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[saveButton]-[nextButton]-|" options:0 metrics:metrics views:viewDictionary]]; [saveButton layoutIfNeeded]; [nextButton layoutIfNeeded]; -(void)prepareButton:(UIButton *)button label:(NSString *) label { [buttonContainerView addSubview:button]; button.translatesAutoresizingMaskIntoConstraints = NO; button.backgroundColor = [UIColor redColor]; button.layer.cornerRadius = 2.50f; [button setTitle:label forState:UIControlStateNormal]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; } 
+5
source share
1 answer

Solved the problem by setting equal heights and assigning priority to VFL:

 [buttonContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[saveButton]-[nextButton( ==saveButton@1000 )]-|" options:0 metrics:metrics views:viewDictionary]]; 

Equal sized buttons in a container view

+5
source

All Articles