How to add Equal Spacing and equal width for button in iOS Auto layout
check this image and made your constraints like below...
RESULT:- preview in different sizes
With Stackview (For iOS 9.0 and above)
NOTE: If you have to make app for iOS 9 and later then
UIStackView
is another option for you
Xcode 7 - Constraints - equal spacing between buttons
To layout 11 buttons equally spaced, put 10 spacer views between them:
Set the following constraints:
- Set a Leading Space to Container View Margin constraint between the leftmost button and the left margin.
- Set a Trailing Space to Container View Margin constraint between the rightmost button and the right margin.
- Pin all of the buttons and spacer views to their left and right neighbors. The constant should be 0.
- Set the height of all of the spacer views (to something like
64
). - Set an Equal Widths constraint between the leftmost button and the top level view. Change the multiplier to
1:18
. This will allow the button size to grow on bigger screens. - Select all 11 buttons (by holding down shift and click on the 11 buttons. Choose Equal Widths and Aspect Ratio. Make the Aspect Ratio constant
1
for square buttons. - Select all 10 spacer views and set Equal Widths.
- Set a Vertical Spacing to Top Layout Guide constraint between the leftmost button and the top margin.
- Select all 11 buttons and 10 spacer views and select Align Vertical Centers.
- Select all 10 spacer views and set their hidden property in the Attributes Inspector.
On the iPhone 4S (3.5 inch):
On the iPhone 6S Plus (5.5 inch):
Autolayout with equal width and setting priority
iOS9 only apps:
This might be a good time to use the new Horizontal Stack View
. Just set distribution
to fill equally
and alignment
to fill
For iOS8 support:
You'll have to set the width constraint to equal width
between the components and the horizontal spacing
to 0
Set button1 to equal width with button2 then button1 to equal width with button3. This should be enough.
iOS Autolayout: two buttons of equal width, side by side
Add the following constraints
- Assign equal width from button1 to button2.
- Assign horizontal spacing between both buttons.
- Assign leading space from button1 to its superview.
- Assign trailing space from button2 to its superview.
- Assign top space to both the buttons.
Let me know if it works for you.
set UIButtons equal distances apart with autolayout
Autolayout doesn't support this right out of the box.
Luckily there is a clever way how to do this. Insert transparent UIView
s between the buttons. Mark these UIView
's widths equal to each other. Now they will take equal spaces between each other.
This is how it would look.
Need assistance setting two buttons of equal width, side by side Autolayout
you can done it using equal width to 2nd button like this
and the output is
Here is apple guide
Related Topics
How to Present iOS Uiactionsheet in Swift
Swift - Check If a Timestamp Is Yesterday, Today, Tomorrow, or X Days Ago
iOS 9 Safari: Changing an Element to Fixed Position While Scrolling Won't Paint Until Scroll Stops
App Killed by Sigkill When Changing Privacy Settings
Force Landscape Viewcontroller in iOS 7
Trigger Local Notifications Automatically Daily on Dynamic Time Given in Arrays Objective C iOS
Optional Chaining in Swift Closure Where Return Type Has to Be Void
How to Stop Multiple Times Method Calling of Didupdatelocations() in iOS
Cannot Put a Google Maps Gmsmapview in a Subview of Main Main View
Table View Images Never Being Released
Xcode Canvas for Swiftui Previews Does Not Show Up
Nstimer Timerwithtimeinterval: Not Working
iPhone Gps in Background Never Resumes After Pause
Including Custom Data into iOS Crash Dumps
Itms-90535 Unable to Publish iOS App with Latest Google Signin Sdk
How to Fetch All Contacts Record in iOS 9 Using Contacts Framework