The scene has a container view inside of a superview, and I've constrained it with respect to the boundaries and 2 text boxes. Instead of "Numeric Value Please", I only see Nu... appearing on there. The console does not give me any constraint related warnings, and I don't understand why the blue view is able to fit in, but the controller is not.
Here are Alert Controller's constraints: http://i.stack.imgur.com/2xhZh.png. It's just constrained to the center.
Another picture of main view's constraints: http://i.stack.imgur.com/2qARq.png
The constraint to the right is too large (125), pushing the right edge of the container to the left.
You should just have: constraint from top (superview), constraint to left (the text field), height (optional), and slightly higher compression resistance. To prevent the text from going off to the right you can also have a >= constraint to the right (superview).
To break the text into two lines, set number of lines to 2 and choose "Word Wrap" + make sure you have a right side constraint and you are not constraining the height (too much).
Another remark: do you really need a "container" view? Why not just a plain UIView? Or does the label have its own controller? That seems like a somewhat convoluted design I think.
Related
I have some problems with autolayout, and I am beginning to wonder if there are bugs in there. I have more issues, but this one seems very clear to me. Constraints are built by code, so I can show only screeshots from View hierarchy debugger.
Setup is easy - KeyboardView as parent, and inside four UILayoutGuides making margins at borders. There is of course more of it, but this should be enough for the issue. When app starts, everything is ok. But after some activating and deactivating groups of constraints by code, I end with this (no error in constraints is reported, layout just breaks silently)
Here is view debugger focused on the KeyboardView (it is the green one). It has a width of 414, which is correct, and is a result of autolayout constraints pinning its sides to superview.
But when I highlight one of LayoutGuides, I see this:
Layout guide has active constraint pinning its trailing edge to the trailing edge of superview (which is the KeyboardView). So it should be inside the green area, at the right edge. But it is totally out! Even if you look at frame, X is at 432.67, which is clearly outside its 414 parent. So the resulting frame constradicts the constraints, but system does not report any problem. I get NOTHING about contradicting constraints and need to break them. Even breakpoint at UIViewAlertForUnsatisfiableConstraints symbol does not fire. Layout just silently breaks without any reason.
Has anyone ever encountered such problems?
Well, I seem to find the problem, and it seems like a bug in autolayout to me. Problem was the grid of spacers (UILayoutGuides) between buttons in the keyboard. Their X and Y centers were positioned to parent's trailing and bottom edges with multiplier - in order to put them at the exact percentage of parent's width or height. Such constraint can't be done with anchors api (cant' use centerXAnchor with multiplier), but is perfectly valid constraint and can be made directly with NSLayoutConstraint() constructor.
These constraints usually work as they are supposed to, but under sume circumstances, after multiple activating and deactivating groups of constraints, they can break autolayout in a very weird way. Like the one that I asked about.
I resolved the problem by using helper UILayoutGuides (yes, even more of them :-/ ) that match its parent's width or height with multiplier. Which is OK; while position with multiplier makes problems, size with multiplier is OK. Then I used trailing and top edges of these helper to pin centers of my spacers to. Such construction now does work perfectly.
I am trying to set margins in the UITableView's cell, but it doesn't work. I set all constraints and nothing. The Labels are always in the same place - left top corner, but in the Storyboard it looks like I want. If I set the layout of a Label to Translates Mask into Constraints is fine, but then I can't set autoresize Labels. I have already made cells using the Automatic layout, but in this case I really don't know what's going on.
Based on the screenshot you posted I believe you're missing a trailing constraint on your City Label that goes from the right edge to the right edge of the content view.
I always make this last trailing constraint a "greater than or equal to" constraint to allow for the length of the content and different device widths.
I made some custom tableview cells and while everything else with them works, the images are really big and I cant get them any smaller.
I did this and its still flooding: I added height and width constraints and a 10 constraint to the left of the ImageView, and selected these settings
I can suggest a few things you can try to achieve it. I have illustrated 1 & 2 on the attached image:
Suggestion 1 is to make sure there is no ambiguity or constraint collision. It seems you have set two constraints for the width of your image. You might be setting it to be 'greater than' the width you want it to be.
Suggestion 2 is to also provide a constraint between the image view and your label view for the heading in order to prevent an overlap.
Suggestion 3 is to make sure that your imageView has a setting of "Clips to bounds' checked on the inspector.
Suggestion 4 Change your content mode to Aspect Fill instead of Aspect Fit
Suggestion 5 Complete your constraints, make sure everything is blue. I don't see top and leading constraint. This will help remove any ambiguity.
Suggestion 6 Use the view debugger, see what constraint values are being used, and the debugger will also show you hierarchies and view layers, which will give you more idea of what is going on.
Your imageView seems to have no x and y constraints. I suggest you to give top and leading constraints for the beggining. You should never see red lines to have stable layouts.
I am working with xcode and I can't seem to get my custom cell to budge. I have the constraints set up and even reset them a couple of times but they continue to look a lot different when I run my simulator.
Here is a screenshot of my constraints:
Here is an screen shot of my simulator:
I have tried deleting the labels and imageView and then adding it again and reset the constraints a few times. Anyone else getting these problems?
You are defining too many constraints. It is key for you to always use THE LEAST number of constraints as possible.
From what I can see you are giving the labels '5' and 'Orders need to be viewed' constraints to the leading edge of the cell. If you are also setting a second leading constraint to the image (hard to tell from the image, code would be easier to debug) this contradiction would break your constraints.
What you want is to have the image leading edge to the cell and give it a height and width and a top constraint only.
Give the '5' label 'horizontal spacing' ( ctrl drag ) to both the image and the second label.
Provide a height and width for it and a top constraint.
IMPORTANT
since you have defined a horizontal spacing from the '5' to the other label, you do not need to provide another leading constraint to the last label.
simply give it a height, width, top constraint and trailing edge to cell
That should be all you need !
If this is too confusing let me know and I can share some snippets for more visualization
I'm trying to learn Auto Layout and playing around with various view combinations. I have various views containing other views, but the one I'm having trouble with is one that contains a label and a text field. They are set up like this:
There are several constraints set up on these, such as center Y alignment between the label, text field, and parent view, 0 leading edge for the label, and a hard coded value (12, but irrelevant) for the trailing edge of the text field. I had initially had it as a 0 trailing edge, aligning it with the parent container. However, the right edge of the text field seems to be cutting off and I have no idea why... I Started increasing the distance, thinking that may be there's a margin issue, but nothing seems to help!
Would love any input.