Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13830

how to adjust space between buttons equally using AutoLayout

I have a main screen with three button vertically aligned. I want adopt my app for 4" screen. so I want to adjust space between buttons equally to fill the screen. see the below images: how can I accomplish that using AutoLayout?

the green area keeps its size.

enter image description here enter image description here

Upvotes: 3

Views: 4357

Answers (2)

jemmons
jemmons

Reputation: 18657

Edit: Apple now favors the use of stack views for this purpose. See this WWDC 2013 video for OS X and this iOS one from WWDC 2015

The Apple approved way of doing this (look for section titled "Creating Equal Spacing Between Views") is to insert hidden views between each of your buttons and set those views' heights to be equal.

See my answer over here for some discussion on why this approach makes sense.

Upvotes: 6

Bimawa
Bimawa

Reputation: 3700

Add each button in view with clearColor for background, align by centerY and CenterX by it subview. setup Constraints like:

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[box][viewForButton1][viewForButton2][viewForButton3]|" options:nil metrics:0 views:viewDic]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[viewForButton1]|" options:nil metrics:0 views:viewDic]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[viewForButton2]|" options:nil metrics:0 views:viewDic]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[viewForButton3]|" options:nil metrics:0 views:viewDic]];

Upvotes: 0

Related Questions