Autolayout with equal width and setting priority - ios

Hi I need to display 3 or 4 buttons based on some condition for that I am using following constraints (image below).
It works fine when I have 4 view, but for 3 buttons, first button is not sized equally, but rest of the buttons are aligned with equal width (image below). I have set priority 990 for horizontal spacing constraint, so rest works fine.
I am setting all the autolayout constraints in storyboard.
How can I achieve equal width when I remove middle view?

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.

Create 3 buttons, and set their width and height equal to parent(Superview).
Next set the multiplier property on "width constraint" for each button with "1/3" value like that:

Related

Xcode How to create a square button(width equal to height) inside a Horizontal stackview

I have a requirement to create 3 square buttons at bottom of the page. All of them must be aligned and their size should be square.
So far, I have created a horizontal stackview and created autolayout constrains 50 from the left, 50 from the right, 10 from the bottom and set the stackview height as 60.
How can I create square buttons, since the stackview subview width is dynamic and determined at run time?
How can I set an equal aspect ratio for these three buttons?
Don't set the stack view height; let the content determine that.
You haven't said exactly how you want the buttons laid out, but I assume you want them equally distributed.
Put the buttons in a horizontal stack view
Constrain the leading, trailing and bottom edges of the stack view to the superview
Set the stack view's alignment to "Fill" and distribution to "equal spacing"
On each button, create an aspect ratio constraint between its width and its height of 1:1
If you want a minimum height for the buttons, set a >= height constraint on one of them
If you want a fixed height for the buttons, set a = height constraint on one of them
I think the only thing you are missing is setting the Distribution in Attributes Inspector on your Stack View. Set it to distribute equally as shown in the image below:
To set the height, you simply need to set a height contraint on each button. Click the tie fighter looking icon and set the height for each:

iOS equally spacing views

Hi, Why is this come out this way on iPad 2 air?? I followed this.
I want the white placeholders to be smaller in width.
If you want all views to be equally spaced, use horizontal UIStackView and set the spacing property to equal.
Or if you want to manually do this via constraints, you have to have to set your views' width proportional to the device/superView's width.
For example, if your iPhone/iPad width is 320, if you want 4 views to be equally spaced, divide the screen width by the number of views while setting equal width constraint to the views:
320 / 4 = 80 (Width of each view)
Do the below steps for all your required views/buttons: (I am using buttons for example purpose)
This is where we begin:
Step1:
Set Top and Leading constraints of your Button1 to its superview like this:
Step 2:
Make your button Equal Width and Equal Height of its superview.
Step 3:
Now your button would apparently be of same size as your super view. Here comes the fun and easy part. Make your button's width 1/4th (If you have 6 views that needs to be equally spaced, make it 1/6) of your superview's width by editing the constraint.
Step 4:
Apply constraints from previous steps to all the views/buttons and update frames to see the change.
These are the constraints for the first button for your reference:

can't display the label in full using Auto layout and stack views

i have a stack view that has 3 buttons and 3 labels. The label's width is equal to the button's width so it's not showing it in full. How can i change the width of label so it can be displayed in full.
You can change autolayout multiplier in autolayout settings to more greater value.
Choose your label in storyboard, in left bar you'll see autolayout settings. Choose width, it must be equal to button with multiplier's value 1. Change it to greater.
Also you can try to make font less.

Constraints not working

I am not sure why but I have having so much trouble with constrains and auto layout. I have the below view and I want to make it display as is shown but every single approach I take to get seems to be incorrect.
I have been following this tutorial online Auto Layout Tutorial in iOS and I am rather trying to describe my constraints. Following this technique this is what I have:
Description of my constraints.
Label:
Centered in the view
51 from the top
All images:
Must have equal height and equal width.
Are separated from the left and right by a 0 gutter.
All inner gutters separating the images are 12.
I have also included my storyboard.
storyboard link
for this you just declare height and width of the first imageview with respect ViewController use equal heights and equal widths and then change in the multiplier default it will be 1 adjust to your size then for the remaining imageviews you just give equal widths and equal heights of the first imageview.
If you want keep height and width constant , don't pin it, just use horizontal centre constraint. Pin it up using auto layout , hope this is helpful.
You are set image height and set image bottom constraint and does not set label height that you are many choice
Set your label height because you run iPhone 4 or 4s that your label doesn't display.
remove your image bottom constraint because you are set fix image height.
you image doesn't fix height that remove your set image height and all image select and set equal height constraint (all image height same).
Better You should move that four image views into a uiview(childView) .Make sure that 4 image views are the subviews of UIView.
-Add equal width and hight constraint to SuperView from the childView.
- Add multiplier value for the EqualHight and Equal Width constraints by selecting both constraints on storyboard(like 1:2.1,1:2.2).it may keep the same distance in all orientation
Add Y position and X position by trailing space and leading space, centre vertical in container.
Then Select the 4 uiimageviews and make sure they have same width and hight,after that
select 4 image views then click the pin button and check the equal width and equal hight.
Add leading ,top and trailing space for image1
[1,2]
[3,4]
add trailing and top space for image 2
add leading ,top and bottom space for image 3
add trailing ,top and bottom space for image 4,

Equal width and equal spacing between buttons in autolayout

I'm trying to use autolayout constraints to automatically resize a few similarly-sized buttons in a view to give the following effect:
Before resizing
Desired effect after resizing
As you can tell, I want the buttons to be of the same size and I also want the spacing between each button to be a constant 20 points. It seems pretty simple at first, so I set the following constraints:
Buttons: space from left neighbour = 20 (inclusive of left-most and right-most buttons)
Buttons: space from right neighbour = 20 (inclusive of left-most and right-most buttons)
Buttons: same width
What actually happens after resizing
When in preview or when I test run the app in my iPhone/simulator, the button resizes and doesn't even follow the same width constraint I set for it. In fact, the view containing the views also resizes to fit the new button sizes. Anyone knows how to fix this problem purely in the interface builder?
Setting:
- equal widths of all buttons
- horizontal spacing between all buttons
- leading to superview for the first button and trailing to superview for the last button
should do the job. Unless you're having problems with the superview (e.g. ScrollView missing constraints)
In the interface builder you set the spacing constraints between buttons like you described above. Then you can command-select all of them and specify the "Equal Width" constraint to apply to the selected objects.
Finally I have oblivion how to solve this problem. I've test it works like charm.
add constraints to space items with 20 units margin
add constraint to ages
now tricky part
for each item add constraint equal widths to a parent
select all this new constraints and change its properties
set multiplier to value 1:5
set constant to -24 (6 separation between items and parent edge gives 120, this multiplied by multiplier value 1:5 gives 24)
update items frames
That's it! Picture below show how it works in interface builder:
Set simulated size to "freeform" and test different widths (I sett this to 330).
This problem is seems to be because of wrong content hugging priority and content compression Resistance priority. So you should set them as low content hugging and high compression resistance (all should have same value).
Because content hugging is the property that resist a view to grow and content compression Resistance priority is to resist a view to shrink. For more information regarding these you can found this Question.

Resources