What's wrong with my auto layout constraints in my xib file? - ios

I have a xib file that I am planning to load into a UIScrollView in my UIViewController.
Here you can download the xib file: http://s000.tinyupload.com/?file_id=13327266143771794775.
It looks like this:
The views with the number 1 are the views with a fixed height.
The views with the number 2 are the views with a variable height. Those views must be able to expand as much as they want, creating eventually a scroll.
I don't understand why it says some constraints are missing. I tried to change priorities but without success.
What do I do wrong?

Since the below views are the subclass of UIview you need to give a constant height for either any one of 2's as shown in below image. As uiview is a container
and if you want the both heights equal select both the views and make the equal width
use Remove at build time if the content will be added programmatically

You should add height constraints to comments View and write comments View. In size inspector Tick the "Remove at build time" for these two height constraints.

Related

Design Self-Sizing UITableViewCell using .xib, cell doesn't adjust itself in interface builder

I'm trying to use .xib file to design a self-sizing UITableViewCell. However, the cell doesn't adjust it's height according to it's content subviews. I have to adjust cell manually. If the cell is too large, a subview will be stretched to fill the cell. And if the cell is too small, there will be some constraint error alert by interface builder.
The UI is correct when running, but is broken when designing.
When using a .storyboard file to design a self-sizing cell, it works great. The cell can grow or shrink automatically.
Is this a limitation of the .xib file? Or did I miss something?
If you set constraints properly, the UILabel will be set its height and width as per the set constrains and its content.
All you need to do is...
For the first label you need to set Leading, top and trailing constraint
For the second label you need to set Leading, top constraint with the bottom of first constraint, and finally trailing and bottom constraint to the superview.
Do not forgot to set the number of line equal to zero & Line break mode to Word Wrap in the Attribute Inspector.
Also need to set the following
We could consider it a "limitation" ... but it's more a matter of "by design."
When laying out UI elements in Storyboard (including a Prototype Cell), Interface Builder (IB) does a lot of additional work. For an obvious example, you can change the Orientation and your prototype cell will automatically expand its width.
When developing a XIB, though, IB doesn't change your root view's size on its own. Think of it as IB saying "you're designing a XIB, you know what you're doing and you know exactly what you want."
While it's generally a good idea to follow IB's advice and get rid of any "constraint error alerts" it's not required.
There are legitimate instances when designing a cell (or other view) in a XIB where you cannot eliminate all constraint warnings. For example, if I put a UIScrollView in my cell XIB, but the scroll view will not have any content until run-time, IB will tell me it has "Ambiguous scrollable content size." In this case, I know my constraints are correct, and I know the content size won't be defined until I add and constrain subviews at run-time... so I can ignore the warning.
If you find it a hassle to manually resize the cell, but not see constraint warnings, you can (for this example) if your bottom-most constraint is Zero, give it a Priority of 999, or set it to >= 0 ... or, change the Ambiguity option to either:
or:
First set top, left, right constraints of first label and bottom, top, right, left constraints of second label with cell.
Set Lines of labels to 0 then it will work as you are expecting.

Adding Size Constraints for Views inside a UIStackView

I currently have a UIStackView that I add views to, it fits a maximum of 10 views along with a button. The issue however, is that although the views I add are equal size, the button is not.
I have tried adding a constraint that determines sets the button's equal to 1/11th of the StackViewSize (so that all of the views can fit equally), but it just seems to break all the other constraints.
How can I give the button size without breaking a million constraints?
Currently the height of the button is adjusted with each new view added, but that's just ugly.
First of all, set height and width to that button according to your requirement using AutoLayout. Then select your stack view from the Document Outline(in your view controllers hierarchy) & then in Attribute Inspector set its Distribution to 'Fill Proportionally'. Add some spacing if you want.

UIStackView losing constraints when using custom XIBs

I am attempting to dynamically add views to a UIStackView.
I have a XIB with a full screen UIStackView within. I then have some views - labels and text field/ text views.
The idea here is that I have a class that manages these individual views and that class has it's own XIB file for the layout.
So the class that handles the outermost UIStackView will grab the layout from the individual classes and add it as a subview to the UIStackView.
When attempting to do this, the view appears squashed and not in keeping with the original constraints.
I could offer code but I will explain the constraints.
The outer UIStackView is just stretched as full screen with distribution fill and equal spacing.
An example of an 'inner' view would be a label. I have an individual XIB file that holds a UIView and then a Label within that. The label is pinned to the center of the UIView.
I believe the issue lies with adding custom XIBs to an existing UIStackView. I'm not sure how much more information I give/ is required but I suspect there is something I am missing that someone may spot straight away from the information given.
Any help is appreciated. Thanks.
The UIStackView does exactly what you are asking it to do. If the constraints are setup so the UIStackView fills in the entire screen, then you need to provide content, that when it is equally divided on the screen, has the expected layout, each of the view inserted will be resized to be equally sized.
If you do not know how many views are going to be used, but you need each of your views to take their respective aspect ratio on the screen, then you can use the "Fill Proportionally" option. The UIStackView will add the height of every view, then divide its own height and distribute it proportionally to each view. If you add too many views this too will start to squash your views.
To avoid these kinds of issues, you may wish to put your UIStackView inside of a UIScrollView. You then setup a constraint with very very low priority to set the UIStackView height to 0, as you insert your views the UIStackView will automatically be resized to fit in the different views.
Good luck!

How to make UIStackView with yours SubViews with different sizes?

I wants create a ViewController with UIScrollView, UIStackView (Vertical), UILabel and UITableView. But my problem is that I can't set different height sizes to my Views. I did tried write in the field, but XCode erases and set back value. I did tried all options of distribuitions, but I think that my option is "Equal Spacing".
I've added two UIView to test.
I did can make my screen, but this not works on iPhone device and XCode says that it's impossible adjust constraints automatically. This is my objective:
Some fields needs different height sizes. Some labels need 3 lines, other 1 line.
You need to set a height constraint in order for your view to have a specific height within the stack view. Apple's Documentation
If you add constraints to all subviews i.e. top and bottom it will work correctly. Make sure you set translatesAutoResizingMaskIntoConstraints to false

Set constraint for UIView that is inside UITableView

This is my view hierarchy:
I want to set height constraint for the View, but it doesn't allow me to set it in the storyboard, and when I set it programatically I got a runtime error (nil)
I went through your case in one of my project, which I needed to embed a view in a static table view. Yes, what different here is that IT IS STATIC, and you can create as many sections as you want.
What I did is really simple, you just need to adjust the view as you wish, then uncheck the axis that you don't want it to be auto layout.
For example, I wanted my view to fit with 44 as its height, and didn't want it to scale bottom down way. So I configed it as the picture bellow.
Hope it helps.

Resources