I have anUICollectionView inside aUITableViewCell and I have some issues regarding to Auto layout.
That's the hierarchy of my views.
UITableViewCell
|
-------ContentView
|
-------MyView
|
|-----UICollectionViewCell
Until here, all views are pinned up in his superviews., e.g: trailing space=0, leading space=0, top space=0, bottom space 0.
The height of myUICollectionViewCell is dynamic, based on the number of itens, which I draw usingUICollectionViewLayout. So it has a height constraint which is >= 100.
After the Items are in place, I call one delegate to update that height constraint, so myUITableView is able to calculate the right height for theUITableViewCell.
Even though this are working fine, sometimes I get the auto layout error above.
How can I avoid this?
The cell's contentView has a fixed height (44). That's causing the occasional conflict between the storyboard, and Auto Layout's derived height.
Self-sizing is the best way to go. What you want to do is to get your "collection view" to determine its intrinsic size, so Auto Layout can automatically handle the variable cell height for you.
See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.
He uses labels but the principle is the same. The cell asks its contentView to lay itself out. The contentView asks the label to lay itself out. The label determines its height. Auto Layout determines the contentView's height, based on the label's constraints, and the cell determines its height, based on the contentView's height. The point is that the label doesn't have a height constraint, and your "collection View" would no longer need one either.
Once you adopt self-sizing, it eliminates a lot of code (and size constraints), as containers manage their size, and constraints simply handle spacing.
Related
I have following, completely ruined design after I tried to implement dynamic-vertical UIStackView inside UITableViewCell:
As you noticed, the label on top of this card is not fitting inside cell. Basically, this cell contains following UI components:
The UILabel (that one which is not fitting). Constraints: top, leading, trailing.
UIStackView(distribution&alignment fill) (it shows list of items with price). My custom view which is responsible for showing item's name and price is inserted to this stackview. Constraints: top to bottom of UILabel (that one which is not fitting), leading, trailing.
The UIView (that divider line with constant height). Constraints: height constraint, leading, trailing, top to bottom of UIStackView.
The two UILabel's (Full amount and total price. You can see it on image). Constraints: Top to bottom of divider line, leading, trailing and bottom constraints.
I set most intuitive constraints in XIB file. There are no errors or warnings. But anyway, the cell is not showing full content inside of it. I have following guesses:
This cell is not alone. I also show other custom cells that have different intrinsic heights. I think iOS doesn't understand how to calculate the height of this cell. Then, I implemented methods estimatedHeightForRowAt and heightForRowAt by returning UITableView.automaticDimension. But, anyway, it didn't help.
I think it is because of hugging or compression resistance priorities. That label is not fitting because it has equal compression resistance as other views. So, Auto Layout randomly decides which view should be ruined and just ruins that view. Ok, I set maximum compression resistance to that label and know it is fully shown. But, Auto Layout decides to destroy my UIStackView by clipping texts inside of it, even they have numberOfLines set to 0. I don't want my views to be clipped. I want them to be shown fully.
It seems serious problem. So, how can I solve it. Here is how constraints are set:
I came across a problem when configuing the cell of UITableView. I add two labels vertically in the content view of the UITableViewCell, and I also add constraints for the top, leading and bottom layout attributes:
I think that the height of the cell can be caculated dynamically as I have set all the vertical layout and with the instrinct size of the label, the height can be inferred.
so, I can not understand the error message that IB told me.
The second problem is that the height of cell appear on the IB is not changed with the constraint I`ve make. If I decrease the bottom constraint for example, and it is the label to change its size to fit the constraint, but not the cell change its height.
If you need to add top , leading and trailing(or width) to the 1st label. Then add bottom ,leading and trailing(or width)for the bottom label. Then add bottom constraint for 1st label to 2nd label.Then by selecting both labels, add equal height constraint.It will solve your problem.
The meaning of this conflict is that when your label content is increasing dynamically, which label's content is needed to give more priority before whom.
More precisely it can be said that if you increase one of the label's content hugging priority i.e. 252 then that label's content increment and size will be given more priority for incrementing it foremost. As autolayout executes according to the priority of constraints, it faces ambiguity in terms of increasing the views of labels if you do not set the content hugging priority.
I was trying to explore the new feature of dynamic cell height using auto layout, introduced in Xcode 7 using this link. I have one UITableViewCell like this below
I want cell to adjust its size automatically based on the content in textView and size of image. I set all constraint and given estimatedRowHeight and UITableViewAutomaticDimension as tableview row height. But when i run the app i'm not able to see the UITextView below. Which means that cell height is not getting adjusted dynamically. Do i need to set the cell height programmatically or still i can do it using Auto Layout.
For TextView, it must not be scrollable.
Also, Your constraints are should be provided in such a way that, TableViewCell should get height automatically(there must be vertical spacing constraints between each component, Height of the each component must be there(it may be explicit of implicit height, but it must need to have height)).
Also, estimatedHeight you are providing must be near to actual average height.
If your tableViewCell is getting above things, then and then only it will get dynamic height based on its content.
I have a UICollectionViewCell that has two labels in it - side by side. They are supposed to take up equal width and the height of the tallest should align to the bottom and push out the container so that it stretches to the tallest of the two. What kind of autolayout constraints can I put on these labels to get that behavior without manual intervention? Is that even possible to define using autolayout constraints?
You can certainly use auto layout constraints to size a container view based on the tallest of its contents. That part is easy.
(Here, for example, I'm sizing a container based on which of two things inside it is wider. It's exactly the same problem, in effect.)
The problem here, though, is that UICollectionViewCells are not self-sizing, so it's hard to see what auto layout has to do with anything. You're going to have to size the cell in code no matter what.
My Goal
I'm creating a custom UITableViewCell (MessageCell), and I'd wish to have my UITextView resizing both in width and height whenever the cell changes its size, while keeping some basic constraint like margins to the edges.
Note : I also change my cell's height in the heightForRowAtIndexPath method depending on the content which will be placed into UITextView.
What I tried
Here are all the constraints currently applied & the cell :
I tried :
With a UILabel.
To fix a Vertical Space from the bottom, thinking it would change the height to fit all the constraints.
To override Height with an User Constraint Height >= 43 (my initial height), but the purple-automatic Height is re-created again whenever I do this.
To find a solution on SO first, but didn't find a case like (even if the UITextView's height's resize with autolayout seems to be a frequent issue).
Plenty of combinations of various & random constraints until my fingers bleds.
How it render now
So...
If someone has any clue or guideline to achieve my goal, I'd appreciate!
I might add, I'm totally new to Autolayout & constraints. My reference : Ray Wenderlich's awesomeness
You need to get rid of that height constraint that the text view has. Make sure you have constraints to something above and below the text view (usually the top and bottom of the cell), and then you should be able to delete that height constraint.