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

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.

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.

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)
For Button 2 : Set Constraints: (1)Trailing,(2)Top or Bottom asper your need, (3)Height
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 :)

Stack layout in iOS9, will do the job really nice.
Add stack view to your view and configure as follow:

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

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.

Related

How to allow automatic resizing of UIButton to satisfy spacing constraints

I'm working on a calculator app that looks like this so far:
The clear button is constrained to 8 pixels below the text field that's above it (which also has constraints that keep it 8 pixels from the edges in the top left corner), the 7 button is constrained to 8 pixels below the clear button, etc. and all of these buttons have a constraint that says they are all equal heights.
My goal is to add a constraint that says the 0 button is 8 pixels above the bottom of the parent view, and then have all the buttons automatically resize to maintain the size and spacing constraints between them. However, adding this constraint just generates some conflicts rather than resizing the buttons. Is there a simple way to tell my app to resize the buttons to satisfy constraints? This is a picture of what I'm hoping these buttons would look like if all goes as planned:
Thanks so much!
Your problem is as a reason for varying in screen height satisfying your constraints can only be done by letting one part (Above 4 blocks / 0 button) to resize itself according to the constraints set for the other part , So Insert the first 4 blocks (4*4) in a UIStackView (inside it 4 horizontal stackViews ) and make it Fill Equally
then when you give any height to 0 Button the above stackView will resize accordingly
Note : I know there is a button on the bottom - right that spans 2 rows , no problem add it above the design i mentioned

How do I properly set my constraints for an adaptive layout?

I am extremely bad at creating layouts for multiple screen orientations. All along I've been using suggested constraints on Auto-Layout but sometimes they don't work as desired. I've been trying to learn how set up my layouts properly by reading and watching tutorials, but I still cannot understand how exactly constraints work!
What I am trying to do is to make the button widths equally like so in iPhone 7:
But on other devices such as iPhone 4S, the second button width shrinks, which is not what I wanted:
Can someone please explain to me how constraints work in layman terms? I do not understand things like:
Leading/Trailing Space
Constrain to margins
Equal Widths and Heights
Top, Bottom, Baseline etc. (What do they even mean?)
Here's how to make two equal-width buttons:
Create two buttons
Add a vertical position constraint to each button. For example, you could position them in the center of the parent view using the "Align->Vertically in Container" function (in the lower-lefthand corner of the interface builder window)
Select both buttons. Add an equal width constraint between them using "Add New Constraints->Equal Widths"
Constrain the left edge of the left button to the leading edge of the parent view
Constrain the right edge of the left button to the left edge of the right button. Add space between the buttons using the "Constant" property of the constraint you created.
Constrain the right edge of the right button to the right edge of the parent view
Hope that helps! 😀

How to Align four buttons at dead center in Xcode 7?

How do I align these four buttons to be at the center (horizontally and vertically) for the diff sizes of screens. I see different sizes and positions of the buttons when I run the project in the different size of the simulator. Is there a way to make the four buttons to be at center (and same size) for ALL devices?
I don't have any experience with constrains, it has been a while since I used Xcode. I read tutorials about constrains for hours, but I still don't understand how to use it.
Screenshot
Thank you and happy holidays :)
The easiest way to accomplish this is to use a UIStackView. Very simple to use, and it automatically manages the layout for the buttons.
Remove the existing constraints for your buttons, then select all four and choose Editor -> Embed in -> Stack View.
Add two constraints to center your stack view in the view, and you're done!
Yes you can use Stack View, but it is only supported from ios 9, so if you are trying to have constraints below ios 9, follow below steps -
Add a UIView as container for your buttons, and add following constraints to it. (We will not give fixed height constraints to this view,let it determine its height intrinsically i.e we will use buttons to determine the size of container as buttons have their intrinsic size.)
a. Align Center X to superview
b. Align Center Y to superView
c. Trailing and Leading space
Now add first button and add following constraints -
a. Center horizontally in center.
b. Top space from superview (as buttons and have its intrinsic size so we dont need to give height and width, it will calculate from its content)
Now for rest of three buttons add following constraints -
a. Center horizontally with first button
b. Add top and bottom space to all buttons with their respective top and button views (buttons). For last button i.e 4th add bottom space constraints with container view.
Note - Now that top button have top space constraints with container view and last button have bottom space constraints with container view and all remaining buttons are connected to each other with top and bottom space, this will determine the height of container view and your buttons will be always appear in center of screen for all size.
Below is the link for screenshot with all required constrints explained above.
Screenshot with required constraints

Issues with AutoLayout

The Bar at the top is a view in a view that contains 10 views, each the same size however I am unable to get them to look like this on the simulator. What constraints settings should I be using?
Also is there a better way to have visually represent all of these boxes without using views? it can't be an image as the individual views will visible depending on remaining battery.
What I am using: I am not currently using any constraints as I was only able to make it worse.
What it is doing now:
portrait:
Landscape:
You just need to give leading, trailing and equal-widths constraints between adjacent buttons.
Check my answer here, I've explained with screenshots and steps to achieve this:
What is the best approach for horizontally aligning 5 buttons with Autolayout
Let me know if you want more clarity.
Select all the bars at once and then apply the following constraints:
Check the equal width. which will make all bar of same width. 5 is the margin from top, bottom, center, leading and trailing.
Press Add x Constraints.
At the end update frame by pressing option + command + equal simultaneously.
The first has left, up and bottom constraints and then it is linked to next one on the right and the next is linked to the next and so on with the horizontal leading option.

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

Using auto layout I am trying to create two buttons on the bottom like this
After dragging two buttons I set constraints like this
On Back button I set Leading Space to Container Margin and Bottom Space to Bottom Layout Guide
On Go To Settings button I set Trailing Space to Container Margin and Bottom Space to Bottom Layout Guide
Then I ctrl+Drag from Back button to Go To Settings button and set the Equal Widths constraint and I get this.
Then I update the Back button width same as Go To Settings button which is 101 and all constraints turn to blue.
But I want both buttons to cover half of the screen no matter what the size of screen is, same as shown in first screen shot, How to accomplish this?
you can done it using equal width to 2nd button like this
and the output is
Here is apple guide
You can have the equal widths constraint between the buttons, but you also have to tell them which is that width you need.
So drag from a button to the superview, add an equal widths constraint. Then in the properties of the constraint you've added, set the multiplier to 0.5, this way your buttons should be on half the screen.
You need to add one more constraint - set horizontal space between buttons to 0
You can followed the layout changes. you will get what you want.
Layout for Back button.
Layout for Go to Setting button.

Resources