1

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?

Superwayne
  • 1,146
  • 1
  • 12
  • 22
  • Have you tried `func reloadItemsAtIndexPaths(indexPaths: [NSIndexPath])`? – hzwzw Mar 02 '16 at 17:57
  • I tried it: For a moment the cell height will update to the correct moment. But then cellForItemAtIndexPath is called again, the cell height resets and reloadItemsAtIndexPaths is called again. It results in an endless loop of resizing. – Superwayne Mar 03 '16 at 11:34
  • In which method are you changing the constraints and adding a button? – Keerthi Polepalle Mar 05 '16 at 07:22
  • In GetCell. I need 0-5 buttons which are aligned to the left side of the cell. I think I will just make two prototype cells, one with buttons and one without. – Superwayne Mar 06 '16 at 16:44

2 Answers2

1

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?

Community
  • 1
  • 1
keshav vishwkarma
  • 1,832
  • 13
  • 20
  • That is not useful for me as it's not using auto layout. I did height calculation in code before but that's not what my boss wants. – Superwayne Mar 12 '16 at 09:33
  • That solution is using the auto layout otherwise subviews of collectionView cell will not display properly. – keshav vishwkarma Mar 12 '16 at 11:12
  • Collectionview does not have any concept like tableView have for height - UITableViewAutomaticDimension, instead of it, CollectionView works on the concept of UICollectionViewLayout. By default collectionView has UICollectionViewFlowLayout. so if you don't want to use sizeForItemAtIndexPath then you need create Custom Layout. – keshav vishwkarma Mar 12 '16 at 11:22
  • For custom Layout you can refer link, It'll help you more : https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest – keshav vishwkarma Mar 12 '16 at 11:24
  • The comment on your linked answer already says it. You don't need to use sizeForItemAtIndexPath with autolayout, as long as the constraints are set correctly. If you constrain either the width or the height, the other will be calculated by autolayout (as long as you're using flow layout). – Superwayne Mar 12 '16 at 17:47
0

==> 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.

Akash
  • 461
  • 2
  • 14
  • That was the previous approach (calculating the size manually and using sizeForItemAtIndexPath). But now it should be layout itself. The first constraints I set work well (every cell has the size it needs). But I can't tell the UICollectionView to look for changed constraints in the cells. For UITableView I did the same. Updating the constraints at runtime and then call beginUpdates followed by endUpdates. That caused the tableview to update the row heights for the new constraints. I am searching for something similar in UICollectionView. – Superwayne Mar 03 '16 at 09:25
  • "But I can't tell the UICollectionView to look for changed constraints in the cells."...this is what I actually want – manish_kumar Apr 28 '18 at 18:59