Ronak Shah
Ronak Shah

Reputation: 1058

How do I use autolayout to align these two UIButtons?

I have a custom UIView XIB file, in which I have placed two buttons:

Image of Buttons Here

How can I progammatically (Swift) or using the interface align these buttons to look like this?

Currently, I get this

Current Buttons (BAD)

Following Alexander's method, i did this, this is my current constraints and workspace.

enter image description here

Upvotes: 2

Views: 412

Answers (4)

Shobhakar Tiwari
Shobhakar Tiwari

Reputation: 7892

Just Follow this step :

Step 1.select LEFT IMAGE and set these constraint

  • Left
  • Top
  • Bottom

Snapshot show : enter image description here

Step 2 : Select Right Image and set these constraint : - Right - Top - Bottom

Snapshot here : enter image description here

Step 3 :Select Left Image and CMD+ Drag mouse from left to right image and select SELECT HORIZONTAL SPACING

Snapshot here : enter image description here

Step 4 : Select Left Image and CMD+ Drag mouse from left to right image and select EQUAL WIDTH as image shown in step 3.

Solved !

Hope it helps.

Upvotes: 1

Alexander Doloz
Alexander Doloz

Reputation: 4188

Add such constraints in IB: equal widths

equal heights

pin top of one button to top of other

also for bottom

set left, top, width and height for left bottom

Upvotes: 0

brkr
brkr

Reputation: 1272

  1. First "control" click & drag from one button to the other and set it like in IMAGE 1 below.

IMAGE1 - https://i.sstatic.net/XJzDv.jpg

  1. Set pins to the opposite corners and unclick "Constraint to Margins"

IMAGE2 - https://i.sstatic.net/sE2zu.jpg

Upvotes: 0

Jasmeet Kaur
Jasmeet Kaur

Reputation: 1568

Add following constraint to Button 1:

  • Leading space to superview
  • Equal Width to Button 2
  • Equal Height to Button 2
  • Horizontal Spacing to Button 2

For Button 2:

  • Trailing Space to SuperView
  • Horizontal Spacing to Button 1
  • Equal Width to Button 1
  • Equal Height to Button 1

Upvotes: 1

Related Questions