AutoLayout in iOS using storyboard only - ios

I have a ImageView whose top constraint is 80 and center. I have two textView with margin of 100 from top consecutive object and 100 margin from both left and right.
When I am trying to implement auto-layout i am not able to do so because of many warning as shown in the figure :-

your imageview and textviews are not comfortable with your current constraints .if you add top and centre constraints for uiimageview you must add the height and width constraints for accessing the x value.
and also you just add height constraints for both of your textviews and try again. thankyou

Every time when you give constraint do following things :-
Fix the width and height of the element.
Give one vertical constraint and one horizontal constraint.
Don't do over-constraint.
Update it where it is showing warning.

If you set Left/Right margin and CenterX, your system is over-constraint.
You should set : Left + Center or Right+Center or Left+Right.
Next you should change the content Hugging and Compression priority.

Related

Can't change the size of View Element in Xcode (Swift)

I am new to iOS Dev and Xcode.
I was trying to practice Auto Layouts in Xcode.
So I was faced with a problem where I wanted to have my score label 20 points from the left side of stack view. So I embedded my label in a UIview element and was able to constraint it to 20 points from left. However, it seems like my UIview element's size has been locked and I am unable to reduce its size.
I want my view element to take same height as my score label which is like 71.5 points but it turns out that minimum height that can be reached for this UIview element is 333.5 as you can see in the image above.
So, how can I reduce its height and did I do anything wrong?
----------- Updated: Here are the constraints -----------
You can delete the MyView.top = top constraint.
Your "Score:" label already has top, leading and trailing constraints to MyView, so add a bottom = Score:.bottom constraint. That will tell the bottom of the white MyView to stick to the bottom of the label.
You may also need to set the Score label's Content Hugging vertical priority to Required (1000).
That should do it.

How to use auto layout to resize views in a table view cell?

I have a cell in which I place four buttons and four labels. Each button gets assigned a picture with width 50 and height 50. Furthermore, all buttons have a corresponding label describing what they're intended for.
My objective is to have the buttons and labels resize to keep the buttons' and labels' aspect ration intact while the screen dimension changes on different devices. I have been playing with auto layout changing the hugging and compression to achieve this but haven't been successful yet. Any help would be much appreciated...
I think you should take a look at a UIStackView, because this seems exactly as a use case for stack. Just put each pair button/label in a stack, and then all four pairs into a horizontal stack, which you constraint to the cell itself. You should be able to handle all you need just by configuring the stack’s properties (axis, distribution, alignment, spacing).
Embed your button and label into a view. Set the width of this view equal widths to content view and change the multiplier value to 1:4. This will adjust the widths of the views according to superview. Also, set the top and bottom constraint to 0 for this view.
Provide center align y-axis constraint to button after setting the width and height constraint to 50. Set its top constraint to a value you deem fit.
Set labels's leading and trailing constraint to a value like 8. Choose center alignment for text. Also, provide top constraint to buttona nd bottom to its superview.
Copy the view and paste to create the three views and provide them equal widths constraint to the first view. Also, provide their leading, trailing, top and bottom constraints.
Here are a fast tutorial in how to achieve that:
1-
2- completion of the first Gif:
Note you can achieve the same output using a UIStackView

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,

iOS UITableView cell fit to screen width

I'm new to iOS and trying TableView for the first time.
I added a prototype cell into the TableView. It has label a inside it which is on the right side. When i run it on lower display like 4S, the label is not visible.
I tried setting Horizontal Space - Content View to label (on right side), but has no effect.
I been playing with the auto layout for sometime, but can't figure it out, can someone be kind enough to help me?
Edit:
Before answering straight, I would say stick to autolayouts and not look for frames, springs and structs.
Now pin UILabel. Select UILabel Go to Editor>pin leading and top space to superview. Also pin the width and height (fix them if you want to). Your problem is solved.
Go through this book for autolayouts and keep playing.- iOS Auto Layout Demystified, 2nd Edition
Updated after question edit :
For Label1 set :
Pin Leading, Top and Bottom Space to superview.
Pin the height(not the width)
For Label1 set :
Pin Trailing, Top and Bottom Space to superview.
Pin the height(not the width) again.
Now select both the labels and go and pin horizontal spacing between them. And if meanwhile you update frame... do update constraint also.
give vertical constraints to your label, if there is not that much need of autolayout you can disable autolayout.
The constraint is not enough to determine the vertical position of the label.
Two optional ways:
Add the Vertical Center in container constraint
Add a height constraint of the label and a top spacing to containe constraint (the contentView of the UITableViewCell your case)
With each of the above and your already exist constraint, the position of the UILabel can be determined exactly.
You can add leading, trailing, top, and height constraints on your label.

Keep UILabel centered using Auto Layout

I'm trying to keep my UILabel centered horizontally. I have tried in IB setting leading and trailing, but that simply stretches out the entire label, and I need it to stay the same size. Everything is set up using Editor and the Pin function in IB and not coding. Suggestions?
If you want to keep all the time buttons together and aligned to the center. Put them all in their own view, arrange how you wish. Then fix the length and width of the view and center it in the container like Aaron suggested. I do believe you will need to make one more constraint. Perhaps pin the view to the top. Hope this helps.
Seems too obvious, but did you try nsconstraint of width = XXX. Label should say same width and the leading trailing should keep it put.
For keeping a UILabel horizontally centre, add the following constraints:-
Width constraint to the label.
Height constraint to the label.
Top/Bottom Constraint for placing the label vertically.
Horizontal centre in container.

Resources