Bojan Bozovic
Bojan Bozovic

Reputation: 910

Get same result with NSLayoutConstraint as with Autosizing

In my UI I have 5 buttons at the bottom. With autosizing applied to every button like on the picutre:

enter image description here

I get desired results:

enter image description here enter image description here

However, when I tried to do it with Autolayout in IB or in code like this:

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_button1, _button2, _button3, _button4, _button5);
NSArray *constraints = [NSLayoutConstraint
                                  constraintsWithVisualFormat:@"|-[_button1]-[_button2]-[_button3]-[_button4]-[_button5]-|"
                                  options:NSLayoutFormatAlignAllBaseline
                                  metrics:nil
                                  views:viewsDictionary];
[self.view addConstraints:constraints];

I get this:

enter image description here

Even when I try to set default width, I don't get behaviour I expected.

Upvotes: 0

Views: 120

Answers (2)

Mike Pollard
Mike Pollard

Reputation: 10195

@"|-[_button1]-[_button2(==_button1)]-[_button3(==_button1)]-[_button4(==_button1)]-[_button5(==_button1)]-|"

Gets you all equal width buttons, although they will stretch to fill the space in landscape...

Upvotes: 2

Prasad Devadiga
Prasad Devadiga

Reputation: 2589

You need to set the equal width constraint to all the buttons and set the horizontal space constraint between each buttons through IB.

To set equal height
* Select all buttons
* Editor -> Pin -> Width equally

Hope this works well, as it worked for me.

Upvotes: 2

Related Questions