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.
Related
I'm trying to add some constraints to my view, but everytime I switch the device, the view goes crazy. How can I fix this?
How I want it to look and how it looks like on the iPhone 8 Plus:
How it looks like when I switch to iPhone SE:
I added these constraints:
0 top
0 left
0 right
686 bottom
If your bottom constraint is larger than the height of the device, naturally, it will be out of bounds.
Moreover, your constraints will be ambiguous if the screen's height is smaller than 686.
You'll need to have a height constraint with a constant of 50 to set up on the view rather than a bottom constraint.
It's not preferable to have such a huge value for any constraint. So when changing devices, the height of screens may change and your constraints will break.
The preferred way will be to create a Height constraint and set it to 686 and don't set the bottom constraint as it's not needed anymore. But the problem can arise here if the screen size is smaller, as the views on the bottom will not be visible. To overcome this you should make use of a scrollview and put this content view inside scroll view.
you can set the Height constraint of View instead of bottom constraint
or
you can use the Size classes
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
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.
I have a viewController, which is configured for the iPhone5 screen size. In red rectangle is the view.
To make this view show properly in iPhone4 - I used auto layout. But when I specify constrains such as: leading edge, trailing edge, bottom space and top space (in top space I even made constraint's priority to LOW ) - the view still goes partially down the screen as if my constraints don't work.
BUT if instead of top space constraint I specify view's heigh and delete top space constraint - everything works perfectly.
Does anyone can explain it please? Thank you.
Thats because when you set up the top space constraint it will move the view by the constant you provided. iPhone 4 and iPhone 5 screen height is different but the constant remains the same so obviously it will behave differently. One way to troubleshoot your interfaces is to switching between iPhone4 and iPhone 5 on storyboard device on storyboard (first button from left to right on the bottom right corner of interface builder).
Auto-layout is all about experience in my opinion. I struggled alot with it until i learned. If you want your view to be attached to the bottom of the screen you should set the BOTTOM SPACE to 0 and specify the view's height like you did or adjust constraints for it's subviews so that the height is set dinamically according to the views inside.
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.