laser2302
laser2302

Reputation: 437

Place four buttons in the center using auto layout Swift 3.0

What kind of constraints should I put so that these four buttons are always in the center without effecting their shapes or sizes. Right now I can't seem to find a way to place them in the center as shown in the picture below.

enter image description here

Upvotes: 0

Views: 595

Answers (5)

MageNative
MageNative

Reputation: 702

You can simply--

1.Take a UIView and give it constraints for horizontally and vertically centered and fixed height and width also.

  1. Add 4 UIButtons in it with fixed width and height as shown in question.

  2. Now add top leading trailing and bottom constraints to all UIButtons.

now your all buttons would be fit as you want......

Upvotes: 0

Pankaj Teckchandani
Pankaj Teckchandani

Reputation: 745

I hope this diagram helps. Don't forget to give width and height constraints to your button as well.

(sorry for bad handwriting)

enter image description here

Upvotes: 1

Jaydeep Vora
Jaydeep Vora

Reputation: 6213

You can group all this 4 view in UIStackView element, And defined the Spacing property between them, set Distribution property to Fill Equally. After that set constraint of Stack view according to you view.

Hope you find you solution.

Upvotes: 0

Max Pevsner
Max Pevsner

Reputation: 4104

One of the options is to create a UIView that is always centered to its superview and has constant dimensions. Inside that view you can put the four buttons with constant sizes and appropriate constraints.

In that way all the button will always be centered.

Upvotes: 1

Ashik
Ashik

Reputation: 1259

You can use UIStackView for this. Place the buttons inside stackView, set constraints in reference to the stackView. Then set constraints on stackView to place it in the middle.

Upvotes: 0

Related Questions