Dynamic height for same content in UITabelviewcell using autolayout programatically - ios

I am creating custom UITableViewCell and adding constraints programatically. But my same text not adjusting with in label and that resulting in different height for random cell. I have looked in to various tutorial and visited answers on other stack questions but not able to solve my problem.
As per my understanding this is the issue of priority between left and right label. I have also added this line while setting constraint.
[righSideLabel setContentCompressionResistancePriority:950 forAxis:UILayoutConstraintAxisHorizontal];

You can set the rowHeight property to UITableViewAutomaticDimension and put the constraint on the Label of your Cell and set the estimatedRowHeight and don't use heightForRowAtIndexPath method.Remember try to put top,bottom,leading and trailing Constraints.

Related

UILabels vertical hugging priority

I have UICollectionViewCell with 2 UILabels that both can have 1 to N lines of text. I would like to use autolayout for calculation of cell height. I have set constraints for labels as follows:
When I run my app, cells are not displayed properly. Layout inspector says that my labels have ambiguous height. I've read quite a lot of possible solutions and tutorials how to achieve self sizing cells, but without any luck.
Thanks for help.
Just set the last label bottom constraint's priority to 749. That's it !!

Scrollview doesn't scroll with multiline label in it

I have a multiline label inside a scrollview. I set up the content size, let's say to scrollView.contentSize.height = 2000
But the view doesn't scroll. There is barely any code in the project. What is going wrong?
The only thing is that I don't have constrain for the height of the label, because it will vary depending on the length of text.
It doesn't matter about the height. But what does matter is that you need to pin it to the bottom of the scroll view also.
By pinning it on the top and bottom it will use the label to set the content size and so allow it to scroll.
I suggest to add a UITableView instead of UIScrollView, adding one UITableViewcell that contains a UILabel. By setting the appropriate values of:
Label's constraints.
tableView's rowHeight.
tableView's estimatedRowHeight.
It should works fine for your case.
For more Information about setting a dynamic cell height, you might want to check this answer.
Hope that helped.

UICollectionView Update Cellsizes after changing constraints

I am using autolayout to determine the cellsize (fixed with, variable height) and set all the necessary constraints in the storyboard. Autolayout works fine as long as I don't change the constraints.
But at Runtime I have to add buttons to some cells. I remove the bottom constraint of the last label, insert the button and add a constraint to the label above, one leading constraint to the cell and one to the bottom of the cell.
The problem is, that the cells size is not updated after this. I tried to call LayoutIfNeeded in the cell and in the collection view but that did not work. I think the constraints are set up correctly. The button appears at the right position but the cell just keeps the height for a cell without a button. How can I tell the CollectionView to update the cell sizes?
You could refer the below link probably you can get your answer with manually and automatically size of UICollectionViewCell.
How make a collectionViewCell auto size only by height?
==> You can either use the UICollectionViewFlowLayout method itemSize property to set the UICollectionViewCell size, or use the delegate method, collectionView:layout:sizeForItemAtIndexPath: if you need to dynamically set the sizes of the UICollectionViewCells.

UICollectionViewCell inside UITableView Cell and Auto Layout

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.

Setting constraints for dynamic cell

I'm new to using constraints in my iOS projects, and trouble setting the right constraints for my dynamic UITableViewCell. I've tried every combination I can think of but it either won't dynamically change height, or it gives me warnings about ambiguous layouts.
My first label1 is not supposed to change in height, but the other two are. My current constraints achieve the desired effect, but are giving me the warnings seen under.
The warnings go away if I constrain the height of the labels (obviously), but that doesn't solve my problem.
Any suggestions on how to solve this would be appreciated!
First of all you need to set
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0
in viewDidLoad of your UITableViewController
Then you need to set constraints from top to bottom of the cell
top space to superview
vertical spacing
bottom space to superview
height greater or equal
and of course set lines to 0 for every UILabel
Example project is available here https://github.com/MihaelIsaev/SwiftAutoResizableCells

Resources