iOS Autolayout: Two Buttons of Equal Width, Side by Side

iOS Autolayout: two buttons of equal width, side by side

Add the following constraints

  1. Assign equal width from button1 to button2.
  2. Assign horizontal spacing between both buttons.
  3. Assign leading space from button1 to its superview.
  4. Assign trailing space from button2 to its superview.
  5. Assign top space to both the buttons.

    Let me know if it works for you.

Need assistance setting two buttons of equal width, side by side Autolayout

you can done it using equal width to 2nd button like this

Sample Image

and the output is

Sample Image

Here is apple guide

Can't get auto layout to make two buttons side by side stay there

Insert those buttons into horizontal StackView component (which is available in iOS 9 and later) and set alignment to fill. Setup their constraints to have aspect ration on themselves and setup them to have equal width (at least that)... Much simpler approach IMO

see:

https://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views

Autolayout : Align two button side by side and move to right side when there is no image

Using two different buttons inside UIStackView will be very easy & effective:

2 Diffrent buttons inside UIStackview
1st button is hide

Arrange 3 UIButtons(of equal width) side-by-side

There are 2 ways to do this.

Way 1:

With use of equal width constraint of button

Select all 3 buttons and add

top, left , right, height and equal width

Sample Image


Way 2:
With use of Stack view

Step 1: Add 3 buttons.

Step 2: Select all that buttons, Once you selected, click on the Stack button in the Auto Layout toolbar at the bottom right of the storyboard canvas. see below in image.

Sample Image

Alternatively you can embed in From Editor -> Embed in -> StackView

Step 3:
Add Constraints to StackView. like below.
Sample Image

Step 4:
Select StackView, Once selected go to Attributes inspector. Change the Distribution to Fill Equally:

Sample Image

And its Done!

How to add Equal Spacing and equal width for button in iOS Auto layout

check this image and made your constraints like below...

Sample Image

RESULT:- preview in different sizes

Sample Image


With Stackview (For iOS 9.0 and above)

Sample Image

NOTE: If you have to make app for iOS 9 and later then UIStackView is another option for you

Keeping buttons distance proportional to each other while scaling buttons

Constrain your second view's horizontal center and vertical center to the first view's, the constant you set here does not matter but I suggest setting it to how you expect the views to be spaced just so it looks correct in your Storyboard. Create an outlet to both of those constraints in your view controller. In your controller's viewDidLayoutSubviews() update each constraint's constant to it's matching axis(x = width, y = height).

@IBOutlet weak var secondViewCenterXConstraint: NSLayoutConstraint!
@IBOutlet weak var secondViewCenterYConstraint: NSLayoutConstraint!

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
secondViewCenterXConstraint.constant = secondView.frame.size.width
secondViewCenterYConstraint.constant = secondView.frame.size.height
}

centerXConstraint
centerYConstraint
iphone
ipad

You'll see in my screen shots I have the second view's top left corner matching the bottom right corner of the first view. If you want to space them more similar to what you have in your screen shot then you could use something like below. I'm just taking a guess on the calculations, you'll have to edit them according to the spacing you want to accomplish. But all calculations you make for these constants should be based on some kind of multiplier of the view's width and height.

secondViewCenterXConstraint.constant = secondView.frame.size.width * 0.9
secondViewCenterYConstraint.constant = secondView.frame.size.height * 0.5

I created a repo to better show my example.



Related Topics



Leave a reply



Submit