UICollectionView Update Cellsizes after changing constraints - ios

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.

Related

UITableView rowHeight UITableViewAutomaticDimension cell wrong height after first load

I've got a UITableViewCell subclass that has two labels of variable height. AFAIK, the autolayout constraints are correct. The first time the cell is rendered in the table it's correct, but any subsequent rendering is blown out too tall. Even after the parent view controller is deallocated! It literally requires an app restart to go back to normal.
I've tried clearing text of the labels in an override of prepareForReuse in the cell class.
// viewDidLoad
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
To support dynamic layout with textview, a height constraint is needed and that needs to be updated in layoutSubviews method(you need to subclass UITextview).
In addition to this, you need to make sure you have a UITextview bottom constraint with cell's contentView.
Sounds like your bottom constraint of UILabel is getting increased. Check if you are updating the bottom constraint constant value.

How to add subviews in Table View Cell while Expanding them and perform actions on their elements of SubViews?

Image Depicting that I have to add this kind of subview to my existing cell and also same subview on multiple clicks of Add icon.
I am facing difficulties in adding subview. If anyone can provide me the correct approach to handle out this structure, help me out.
You can design your cell for a expanded mode in interface builder and set the auto layout constraint correctly instead of adding subview to the tableView cell while cell is expanding.
1) In interface builder design your cell's view for expanded cell and add subviews in a view which you want to be added while cell is expanded.
2) Now set the auto layout constraint correctly and don't forget to set the height constraint for the view which contains all the subviews for your expended cell.
3) Take the outlet of the height constraint, you created in step 2.
4) Now set the height constraint's constant value to 0 for the normal cell and a value > 0 for the expanded cell.
heightConstraint.constant = 100
The easiest way to do this is to treat each addition as a new row and keep track of that, rather than put it all in one cell.
Another way to do this is to add another tableview inside this cell and increase its height based on the number of rows it has. But I am not sure about performance in that case.

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.

How to create UITableViewCell with dynamic cell height based on its content

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.

Managing dynamic height for UITableviewCell using iOS 8

I am trying out autoLayout features for dynamic height for UITableViewCell in iOS 8, I followed this blog http://www.appcoda.com/self-sizing-cells/. So here they mention that from iOS 8 managing dynamic height is hassle free.(In the link they have used swift, but I am using objective C)
Now after doing autolayout from xib, then we need to just write this lines of code in viewDidLoad
- (void)viewDidLoad
{
m_tableView.estimatedRowHeight = 83.0f;
m_tableView.rowHeight = UITableViewAutomaticDimension;
}
This is fine and my tableView height changes dynamically based on the content.
I want to know how to have more control on UITableViewCell height.
1) In my app, I want the cell height to be based on the content(data), but I need to have a default height, which will be applied if the content height is less than default height.
2) I want to know what will be the final height of the cell, before displaying to the user, because I want to add some UI elements below each cell.
So what functions I need to use to fulfil my tasks.
You would need to make sure your Storyboard prototype cell has NSLayoutConstraints from top to bottom. Dynamic cell sizing compresses whatever layout constraints you have in place to their maximum compression given the content set in cellForRowAtIndexPath to calculate the final rendered height of that cell. It is very convenient but it also means you need to be careful about setting your constraints correctly.
If you are adding any subviews programmatically, you need to ensure the same rule of thumb is true- there is a complete, deterministic set of constraints from the top of your cell's contentView to the bottom. Additionally, you will need to set any subviews' added in code translatesAutoResizingMaskIntoConstraints property to NO.

Resources