UILabel position on device vs simulator not consistent - ios

I am testing my app on an iPhone 4 and iOS Simulator (4S). The label positioning is fine on the simulator but displays incorrectly on the device.
I am using the following constraints on the label:
What could be causing this to happen?

You have a case of conflicting constraints. Let's step through the rules you applied to your label:
Align Center X to Superview
This is just aligning the center X of your superview with the center X of your label. No trouble here.
Bottom space to: SIGN UP <= 50
This constraint applies the rule "make the bottom of my label at MOST 50 points away from SIGNUP."
The issue is with your last constraint:
Top Space to: Top Layout Guide >= 5
This constraint adds the rule that the top space of your label must be AT LEAST 5 points away from the Top Layout Guide.
Auto Layout recognizes it can't possibly satisfy both the Bottom Space and Top Space constraints at the same time, so it destroys your bottom constraint, resulting in the bad behavior.
Your top constraint is still valid, because in that case the space between the top layout guide is greater than 5.
Try making your Top Layout Constraint a <=, or lowering the content compression resistance priority of your label.

The answer is in your constraint itself.
It shows that the topspace can be greater than or equal to 5. And bottom space can be less than or equal to 50. So that's why it shows such a behavior.
In my suggestion edit that constraints like:
Top Space equal to 5
Bottom Space less than or equal to 50

Related

Ambiguous constraint for UITableView when width >= and <=

Trying to set table view width between 290 and 460 points.
It's pretty enough to fir content for the small screens and looks good for the big ones, like iPad Pro.
Also I want to have leading and trailing space >= 20.
Today's the second day I've tried to fix "inequality constraint ambiguity"...
Thanks for the helping!
I'm new in iOS dev so please do not kick me too much :)
If I understand your goal correctly, you want your table view to be:
20-pts Leading and Trailing
unless that makes the table view wider than 460-pts
in which case the table view should be centered horizontally
So, to recap my comment... your constraints say to:
Center the table view horizontally
make Leading and Trailing >= 20-pts
make Width between 290 and 460
The problem is that you haven't told auto-layout where between 290 and 460 you want the width to end up.
So, you need a couple more constraints.
Start simple... tableView constrained 20-pts Top / Leading / Trailing and Zero-pts Bottom (all to Safe Area, of course):
Next, add a second set of 20-pt Leading and Trailing constraints:
Now set one pair to >=, and give the other pair a Priority less-than required:
The Default High 750 priority on the = pair of Leading/Trailing constraints tells auto-layout to "try to pull the sides to 20-pts from the edges, but allow that to break if a conflicting constraint has a higher priority", and the Required 1000 priority on the >= pair gives us a minimum of 20-pts on each side.
If we switch to Landscape orientation (iPhone 11), everything looks the same... 20-pts Top / Leading / Trailing and Zero-pts Bottom:
But... your goal is a max-width of 460, so let's add a Width constraint of <= 460:
We see Red issues with the constraints, because we still haven't given auto-layout quite enough information - so, we'll add a Horizontal Center constraint:
and here's how it looks on a 9.7" iPad:
We don't need -- and don't want -- the >= 290 Width constraint. The = 20 constraints will handle that. And, while unlikely these days, if your app ends up on an iPhone 4s or 1st-gen SE, the view width is only 320... and 290 + 20 + 20 == 330 and you'd have constraint conflicts.

How to resolve inequality constraint ambiguity - vertical ambiguous in AutoLayout in iOS

In my iOS application, I have enabled Use Auto Layout option and I'm using storyboards to design my UI. There in one of my View I'm having UIDatePicker with following constraints.
Trailing space to: Superview
Leading space to: Superview
Bottom space to: MyUILabel Equal <= 20
Top space to: MyImageView = 20
The highlighted constraint gives me following warning
Inequality Constraint Ambiguity
When I see the issue using Issue Navigator, it tells me
Ambiguous Layout MyUILabel is vertically ambiguous.
That MyUILabel is having following constraints
Trailing space to: Superview
Leading space to: Superview
Top space to: UIDatePicker <= 20
Bottom space to: AnotherUILabel = 20
Why I need this kind of constraint is when I load the application in 3.5 inch screens some of my bottom UI elements overlapped. By adding that highlighted constraints solve that issue and give me two separate issues.
The first one is the above mention Inequality Constraint Ambiguity and the second one is, even in 4 inch screen size this constraint is getting apply. I mean for 4 inch screen sizes and above I can have Bottom space to: MyUILabel Equal = 20 and it appears fine without any overlapping.
How can I solve this?
Simply I need having <= 20 constraint for 3.5 inch screens and = 20 constraint for 4 inch and above screen sizes.
Thanks in advance.
Its hard to tell without seeing your whole view. Base on your description, I would check your topmost view to make sure its vertically pinned to it's superview. For your case I think that is the "MyImageView"; try setting it's "Top Space to Superview/Top Layout Guide" equal to 10/15/etc whatever you want.

iOS autolayout changes leading and trailing space

I have some issues with auto layout, hopefully some of you can help me =).
I add a UIView to an empty view controller, like this:
Then I add constraints for leading and trailing space, like this:
Which results in this:
I can't understand why it sets them as equal to -16.0. If I change those values to 0, then Xcode complains about the width of the gray UIView.
When I run the app on iPhone 5 simulator, it looks fine:
However, when I run the app on iPhone 6 Plus simulator it adds white space on the left and on the right:
I don't want the gray UIView to have any space from the left of from the right no matter how big the screen is. How can I fix it? Thanks!
Double click the constraint and open it. There select first or second item and deselect "Relative to margin". Then set your constant as 0.
Xcode is showing -16 because it is calculating space with respect to a margin and not the edges.
Check following link for more info on margins. Editing Auto Layout Constraints
You can clear all existing constraints on the view,and in Any*Any mode, add constraints for all 4 space to superview, which are Leading, Trailing, Top, Bottom. Then it will have the equal width with the screen no matter how big the screen is.

iOS Auto Layout >> View is not Changing its Size

I have a design for a screen that should look like this (other things will be added later, but I cannot seem to resolve the basis...):
I have added Constraints to determine the following:
Both Labels are Constraint in spacing to the screen edges.
Middle View is Horizontally and Vertically Constraint to the Middle of the Background View Center.
I have added 4 Constraints to express Minimum and Maximum Vertical Spacing between the Middle View and the Labels (Current spacing as Maximum and Standard spacing as Minimum).
I have also added 2 Constraints to the Middle View to define Spacings from the Screen right and left edges.
I thought that it should be enough, but in reality, when switching between Retina 3.5 and 4 the Bottom Label disappears and the Middle View is cut in the middle:
I have tried lowering the Middle View Content Hugging and Content Compression Priorities, and still no good.
Here are the Warnings I get:
Any idea how to resolve this?
Or alternatively, how to approach it differently (preferably, still using Auto Layout)?
Add Equal Width & Equal Height constraints as well & It will work
Add TopSpaceToContainer constraint for Top Label. Then add width and height constraints for your yellow view at the middle. Remove the multiple vertical spacing constraints given to the Top Label and Bottom Label.

Ambiguity with two inequality constraints

I want to have the bottom view be at least 20 away from both the image and the label above it. The label is multiline, so it can be taller or shorter than the image view, depending on how much text there is. When I add two "distance to nearest neighbour >= 20" constraints like shown in the screenshot, Xcode tells me constraints are ambiguous.
How do I fix it?
I think you can do it like this, if I understand your requirements:
The image view has a fixed width and height, and constraints to the left side and top, as well as a constraint to the bottom view of =20 with a priority of 700. That's crucial -- that will set the y position of that bottom view (which has fixed height and constraints to the two sides), but will allow it to move lower if another constraint with higher priority makes it. That constraint with higher priority is the constraint to the label -- it's >=20 with priority of 1000 (the label also has constraints to the top, right side, and trailing edge of the image view).

Resources