Horizontal constraints in iOS - ios

In my storyboard, my view controller has two container views as shown below
The second container view is it tone am having difficulty setting constraints for.
When I actually run the app, the text field, becomes very narrow. My horizontal constraints are obviously incorrect but I can't figure out the right settings.
Here what I currently have:
FilterIcon - trailing space to superview (right): 0
- Align Center Y
- Leading space to "Go": 16
- Equal height to text box
Go Button - align center Y
- trailing space to filter icon: 16
- leading space to text box: 8
Text Box - leading space to superview (left): 0
- trailing space to Go Button: 8
- equal height to Filter icon
- horizontal hugging priority: highest of 3 elements 751
I thought the text box would expand from the left margin until it reached the Go box which is set to the right based on the filter icon.
Instead I get this result:
What am I missing for the constraints?

How about configuring like this?
Text Box
leading space to superview (left): 0
trailing space to Go Button: 8
equal height to Filter icon
horizontal compression priority: 752
Go Button
align center Y
trailing space to filter icon: 16
leading space to text box: 8
horizontal hugging priority: 251
Filter Icon
horizontal hugging priority: 251
Align Center Y
Leading space to "Go": 16
Equal height to text box
Here is my screenshot.
If you don't want to set priorities, you can just set width constraints for both Filter Icon and Go Button. For more information, there is very good tutorial about content hugging priority and compression resistance priority in here

I think you can set both buttons' height equal to width. Then other space is left to the text field.

Give aspect ratio to the text field for its width and height and give center y to superview. This will definitely work. But make sure when you give aspect ratio, your text field is having the width you require.

Related

Alternative labels giving Fixed width constraints may cause clipping warning why?

I have designed two labels side by side i have given left side label 150 fixed width, why alternatively getting below warning.
For above image left label i have given constant text and constraints like below
fixed width = 150, top = 10, leading = 10 , height = 30
for right label text will come from json and constraints like below
top = 10, leading = 10, trailing = 10, height = 30
i have highlighted all my left side fixed width labels, here for all labels i got below warning
Main.storyboard Fixed width constraints may cause clipping
but here if i select first label warning and if give greater then or equal and horizontal hugging from 251 to 250 then warning disappears.. like same if i do for second label then it becomes big like below
for third label if i give greater then or equal then warning disappears.. like alternatively warning disappears why, you can see that in top image. why its happening like this, how to clear all my warnings. please help me.
1.Give the left sided labels horizontal content hugging priority of 252.
2.Give the right sided labels horizontal content hugging priority of 251.
3.Give horizontal spacing of 8 between the left sided and right sided labels.
4.Remove fixed widths for all labels.
5.Align leading and trailing of all the left sided labels and just the trailing of all the right sided labels.
6.Give vertical space to left sided label from right sided label of the previous row.
7.Vertically center align the left sided and right sided label in the same row.
All your warnings will disappear.

How to prioritize one constraint over another when the font gets larger

I have a UILabel with the following constraints:
- Align Center Y to Superview (priority 750)
- Top Space to View, greater than equal to 0 (priority 1000)
In normal mode, the UILabel is align center to Y which is ok. But if I go to Accessibility setting and set to large fonts (maximum size) I want my label to use the Top Space with greater than equal value.
Right now is still using the Align Center to Y in both cases. Is there a way when the font is large to use the other constraint with higher priority ?
PS: below this label there are 2 other labels which get shrunk when font is large, because top view label stays in the middle of screen instead of moving up.
Try this
- Bottom space to view, equal to zero (multiplier 2)
- Top Space to View, greater than equal to 0
Screenshots
Project - https://github.com/leninsmannath/AutoLayoutBottomFromCenter.git

Content hugging with auto shrink

I have a UILabel with no of lines = 1 and auto shrink set to a minimum font size of 9. The font size of the label is system 70.0
I have increased the content hugging priority to 1000. I did this assuming that the height of the label will fit itself to the text.
But still the UILabel does not resize itself to fit it contents. The height of the label is very large. I want the height of the label to just fit it contents.
Thanks.
The problem is you have given both leading and trailing space, so label will stretch itself to satisfy these constraints, As label has intrinsic size(i.e it calculates size based on its content), you just need to give constraint for x and y position
So, delete your leading and trailing space constraint, just have Align CenterX and Align CenterY constraints, these will be sufficient to give x and y position of your label and you will get desired results.
Edit - Adding Screenshot.
Also understand that here I have given trailing space constraint >= 10 so that label can resize itself according to its content.
Also if your are checking in iphone 6 screen then AutoShrink Minimum Font Size - 9 will not be fit for screen width, try giving AutoShrink Minimum Font Size - 6.
Result of above constraint -
1.For Long text
For small text -

How fill a UITableViewCell with multiple UILabel using auto layout

i have a UITableViewCell that contains some UILabel and i want that this label fill horizontally the width of the UITableViewCell and that doesn't remains big space instead of other, this is an example:
|---------------------------------------------------------
| |
|***This is a uilabel***This is a uilabel***label***lb** |
| |
|---------------------------------------------------------
So with different UILabel width i want that all the UILabel fill all the UITableViewCell fixing the font size if needed, the * is the spacing, how i can achieve this with auto layout?
STEP 1: Set up constraints
Leftmost label: Add a leading edge constraint and a center Y constraint to its superview
Middle labels: Add a horizontal spacing and center Y constraint to previous label.
Rightmost label: Add a horizontal spacing and center Y constraint to previous label plus a trailing edge constraint to superview.
These constraints ensure the gaps between the labels and on the left and right are fixed in size, and that they are all centered in their superview. Because there are no width constraints, the layout system will attempt to size them to fit their content. But this leaves an ambiguity...
STEP 2: Set content size priorities.
The above constraints are still ambiguous -
If the total text content is less than the superview's width, which label will expand bigger than its content?
If the total text content is more than the superview's width, which label will contract shrinking its content to fit?
The answer to the first question is the label with the lowest horizontal content hugging priority. By default they will all have the same, so make sure they have different horizontal content hugging priorities.
The answer to the second question is the label with the lowest horizontal content compression resistance priority. By default they will all have the same, so make sure they have different horizontal content compression resistance priorities.
STEP 3: Ensure text shrinks to fit.
We now have an unambiguous layout. But by default, the labels will truncate their content if they do not have room to display it. Set Autoshrink to Minimum Font Scale or Minimum Font Size on your labels to ensure they adjust font size to fit their content rather than truncating it.

Autolayout not understood operations

here some points about using autolayout that I don't understand.
1/ I have set a constraint on the leading space and the trailing space of a label (so the horizontal size of the label should be adjusted automatically) but the label fit the size of the text. How to not autosize the label (I have seen hundreds of posts about autofit a label but nothing about not autofit).
2/ Concerning my UIScrollview, I have set up trailing space and leading space to 0 (so it should fit all the screen whatever the iPhone display size) but some margins still appears. Why ?
Thanks for your help.

Resources