iOS Autolayout: two buttons of equal width, side by side
IosIphoneXcodeAutolayoutIos Problem Overview
I am currently having difficulty with AutoLayout. I am using interface builder and am trying to position two buttons of equal width side by side as illustrated in the following image.
From the following Preview image, my titleImage has been properly constrained and is displaying correctly however the buttons are not. I have experimented by aligning button 1 with the leading edge of titleImage, and button2 with the trailing edge of titleImage, however the width distribution between the two buttons becomes skewed at this point, as demonstrated below.
My objective is to understand what constraints are missing and should be applied to the two buttons to maintain equal widths regardless of device. If possible I would like to achieve this through interface builder as opposed to additional code.
Ios Solutions
Solution 1 - Ios
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.
Solution 2 - Ios
Follow Steps and Screenshots for easy solution
Step-1)
- For Button 1 : Set Constraints: (1)Leading, (2)Top or Bottom asper your need, (3)Height
Step-2)
Step-3)
- Press Ctrl + Drag From Button 1 to Button 2
- Select Horizontal Spacing
Step-4)
- Select Both Button (using Command) and Add Constraints Equal Width
OUTPUT
Hope it Helps you :)
Solution 3 - Ios
Stack layout in iOS9, will do the job really nice. Add stack view to your view and configure as follow:
Solution 4 - Ios
You can check my example - you can easy aim this, by using proportional constraint. Also you can easy aim proportional multiple UIViews. Please look attached example
https://dl.dropboxusercontent.com/u/48223929/TestViewController.xib
Solution 5 - Ios
My solution is to
- Put a small view in the middle of two buttons and make it centre(Horizontal centre in container and vertical centre in contener as 0).
- Add height and width to the small view.
- Add buttons the constraints and give horizontal space constraints to small view.
- Give the small view background colour same as buttons or View's colour.
Note: See the Screenshot.