A UICollectionView bug?

I have a UICollectionViewController while doing the following:

  • Return a string array in collectionView:indexPathForIndexTitle:atIndex:
  • Set cell.contentView.backgroundColor = [UIColor redColor] on collectionView:cellForItemAtIndexPath:

Notice that changing the background color of the cell is just for demonstrating the issue. The important thing is to show index titles on the right.

The result (as following displayed) is that the content view takes the entire collection view width and it doesn't being affected (as it should) by the appearance of the index titles.

With UITableViewController I am doing similar steps:

  • Return a string array in sectionIndexTitlesForTableView:
  • Set cell.contentView.backgroundColor = [UIColor redColor] on tableView:cellForRowAtIndexPath:

Here, in contrast to the collection view behavior, the content view is "shrinking" as expected as following appear:

Can anyone tell if this is a bug or the expected behavior?

  • You had another question for the collectionView. Was it solved ?

  • Unfortunately not. The issue of my other question is far more problematic and really puzzles me... Can you please help or guide me? I have no idea how to fix it...

Add a Comment

Replies

Could you show how you defined the size of cells ? They seem to be full width of the collectionView. What happens if you reduce by 20.

Similarly, what happens if you reduce Collection width by 20 (trailing = 20) ?

  • I don't set the size for the cells. You can see the code at https://github.com/yoasha/CollectionViewDemo

    The main issue is that index titles in my case sometimes appear and sometimes not. UITableView updates the content view constraints automatically to make room for the index titles when they appear, while the UICollectionView doesn't. I therefore need in collection view to manage this manually rather than let it do the work for me.

Add a Comment

You create the collectionView programmatically.

Did you try to create in storyboard (to be sure you configure all parameters as you want) ?

  • No need for that. Modifying the trailing constraint (as you previously suggested) did the trick. The only issue with this solution is that I need to make some assumptions (where iPad requires trailing of 20 vs. 10 for iPhone) and these numbers can change in future iOS updates according to Apple UI design changes. It could be very useful if UICollectionView provided us API to get the required trailing, to eliminate any assumptions.

Add a Comment