Setting a constraint with dynamic trailing - ios

I am trying to set a trailing constraint that changes based on screen size. Here's the example. Do I have to set a certain priority with >=?
What I want is for the plus button's trailing space to always be the same distance no matter the screen size. How do I set this constraint?

Okay I'll try to answer but I might be wrong since I might miss some information. For me there are two important components, the right most button (with a white cross in the middle) and all the other objects in the left. I assume other objects are somehow grouped and can be treated as a single unit. What you need to do is add new constraints from the storyboard. For the button, you at least need to add "horizontal space to the right" constraint and "width" constraint. For the left hand side, you need to add "horizontal space to the left" and "horizontal space to the right", but don't fix the width. I think this can achieve the visual effect you want. If it still doesn't work you might want to check this tutorial, http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial, as your question is very fundamental.

Related

iOS - Swift 4 - Constraints for text fields side by side

I Have a view like so:
And I am trying to add constraints so the text fields are side by side so the auto looks the same no matter the device width, how would I do that?
I have tried the following for all 6 of them:
But it ends up looking like this:
What am I doing wrong?
Ctrl drag from the description TextField to vendor TextField and select Equal Widths from popup
You can also use a vertical stackView where each row is a horizontal 1 with distribution set to Equal spacing and drag the text fields to it that will be more easier
Well, let's look at what we know about the horizontal axis (I assume you've got the vertical axis taken care of). There are two text fields; call them Left and Right. We know these things:
Where Left's leading edge must be
Where Right's trailing edge must be
The distance between Left's trailing and edge and Right's leading edge
The relationship between Left's width and Right's width, i.e. they are equal
Ta-daa:
(A UIStackView would construct these same constraints for you, but in a simple situation like this it's probably more trouble than it's worth.)
There are many ways to do it
I give you two but recommend the first one in your case.
First :
Define a constraint by draging one of your UITextFiled to the other one and set it to “Equal Widths”
You may also be able to define a percentage for each (60-40%) if you like.
Second :
Define a constraint like above (draging) And define a relative ratio (0.5) for UITextField width against its superview. Do it for the other one. You need to go to Object Inspector Tab and change the values if needed (so this requires two constraints, one for each UITextField as each of them are relative to the their superview) this is also a little bit slower than the first approach since margins should be defined with priority.

NSLayoutContraints: How to position a bottom view below the higher of two labels?

I am working on an iOS 11+ app and would like to create a view like in this picture:
The two labels are positioned to work as columns of different height depending on the label content. The content of both labels is variable due to custom text entered by the user. Thus I cannot be sure which of the the two labels is higher at runtime and there is also no limit to the height.
How can I position the BottomView to have a margin 20px to the higher of the two columns / labels?
Both labels should only use the min. height necessary to show all their text. Thus giving both labels an equal height is no solution.
I tried to use vertical spacing greater than 20px to both labels but this leads (of course) to an Inequality Constraint Ambiguity.
Is it possible to solve this simple task with Autolayout only or do I have to check / set the sizes and margins manually in code?
You can add labels to stackView
One way to do this is to assign equal height constraint to both label. By doing this height of label will always be equal to large label (Label with more content).
You can do this by selecting both labels and adding equal height constraint as mentioned in screen short.
Result would be some think like below
The answer given as https://stackoverflow.com/a/57571805/341994 does work, but it is not very educational. "Use a stack view." Yes, but what is a stack view? It is a view that makes constraints for you. It is not magic. What the stack view does, you can do. An ordinary view surrounding the two labels and sizing itself to the longer of them would do fine. The stack view, as a stack view, is not doing anything special here. You can build the same thing just using constraints yourself.
(Actually, the surrounding view is not really needed either, but it probably makes things a bit more encapsulated, so let's go with that.)
Here's a version of your project running on my machine:
And with the other label made longer:
So how is that done? It's just ordinary constraints. Here's the storyboard:
Both labels have a greater-than-or-equal constraint (which happens to have a constant of 20) from their bottom to the bottom of the superview. All their other constraints are obvious and I won't describe them here.
Okay, but that is not quite enough. There remains an ambiguity, and Xcode will tell you so. Inequalities need to be resolved somehow. We need something on the far side of the inequality to aim at. The solution is one more constraint: the superview itself has a height constraint of 1 with a priority of 749. That is a low enough priority to permit the compression resistance of the labels to operate.
So the labels get their full height, and the superview tries to collapse as short as possible to 1, but it is prevented by the two inequalities: its bottom must be more than 20 points below the bottom of both labels. And so we get the desired result: the bottom of the superview ends up exactly 20 points below the bottom of the longest label.

What is the proper way to avoid warning trailing constraint is missing on button inside the TableViewcell

I want to have UI design like (Send Feedback Button)
First I didn't add Trailing space (my thought was if I add trailing space and if the font got bigger, the text will get truncated, CMIIW)
So to avoid that, I set the trailing space to use "Standard Value", but when I used that and I clicked anywhere in the entire cell, the button got clicked.
So how to get rid of the trailing space warning, without making the entire TableCell clickable? (I just only want my text to be clickable).
Many thanks.
You may add a trailing space constraint with a lower priority (i.e. < 1000). It won't give warning in case the constraint is broken.
I have attached a screenshot from my app for your reference.
Hope this helps
if i am correct, you just want to keep the text tappable.
I think you should not give the height & width constraint to the button , it will manage the size according to the text. Just set the leading & vertical constraint to the content view. I hope it would help.

Strange behaviour in Storyboard (Missing view when running app)

For the last few months I have been doing all of my layouts in code to practice with autolayout.
I have decided to go back to Storyboards and am regretting it already :-). I have been trying to do a layout with multiple views including a UIStackView and was getting many errors. I thought maybe I was doing something wrong with the StackView so I decided to delete everything apart from a button and label.
The simple view with a label and button can be seen below:
As you can see, the constraints on the label are very simple. The button in the top right (where the blue is) just has top space and trailing space. However when I run the app the label is not seen:
I am also constantly getting: An internal error occurred. Editing functionality may be limited.
Has anyone else experienced this odd behaviour?
You have set the leading and trailing of the label . This will definitely happen because you have set the wrong constraints. Check the size of your view Controller where you have set the constraints, its (Any width , Any height).
Now, check in which size you are running, its (Compact width, Regular height). If you run it in a iPad you will find your label. Since, you have not fixed the width of your label, the label gets compressed when it is run. Try setting the width of your label and remove either the leading or the trailing constraint.
Else instead you can fix your width and select either the leading and trailing and set it to "less than equal to " rather than "equals to".
While designing imagine yourself holding the object keeping in mind about all possibilities that can occur and then set the constraints accordingly. A user can run your application in any size iPhone and iPad. So, the design should be such that it doesn't affect your objects in it.
If any problem occurs try view debugging then you can definitely find out where you have done wrong.
You may need to remove label's leading & trailing space, and add add constraint call "Center Horizontally in Container" if you want to center the label.
You can learn more about autolayout in here if you do not know it well.
You have hard coded leading and trailing constraint constants. They add up to 518. That probably makes your label width negative. You don't see it in Storyboard because you're using the inferred simulated metrics. You'll see it right away if you change to an 3.5 inch iPhone.
I think a better approach would be to remove the leading and trailing constraints and center the label horizontally in the superview. You can set a fixed width on your label if you want.

Using Autolayout to set scaling rules for objects

As you can see from the grab, I have all my spacing sorted and am pinning the leading & trailing edges properly; I am stuck on getting my buttons to scale horizontally so the space in-between stays the same. The conflict is all related to the fact that I haven't set a fixed width for the button, but in this case I don't want it fixed.
How can I resolve this autolayout conflict? Does it need some kind of width constraint? That defines a minimum at least?
select both buttons
click add constraints menu
select equal widths
select buttons one by one and add other constraints till all lines turn to blue like image below.
Hope it helps.

Resources