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.
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
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:
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
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.
Alternatively you can embed in From Editor -> Embed in -> StackView
Step 3:
Add Constraints to StackView. like below.
Step 4:
Select StackView, Once selected go to Attributes inspector. Change the Distribution to Fill Equally:
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...
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
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
}
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
What Is Lldb_Expr in Swift Playground
How to Store Push Notification Alert Message in Userdefault
iOS 9 Orientation Auto-Rotation Animation Not Working, But Always on Main Thread
Combine Onchange and Onappear Events in Swiftui View
How to Use Sfsafariviewcontroller with Swiftui
Writing an iOS 8 Share Extension Without a Storyboard
Html5 Video Autoplay on Iphone
<Input Type="Number"/> Is Not Showing a Number Keypad on iOS
Programmatically Get a Storyboard Id
Uitextviews in a Uitableview Link Detection Bug in iOS 7
Use Table View Disclosure Indicator Style for Uibutton iOS
Uitableview with Two Custom Cells (Multiple Identifiers)
iOS 7 Uiimagepicker Preview Black Screen
Fade Out Scrolling Uitextview Over Image
The Advertisingidentifier and Identifierforvendor Return "00000000-0000-0000-0000-000000000000"