2

I have a custom UICollectionReusableView with a single label inside: the label has leading, trailing, top and bottom constraints set to 8.

On the controller, I register the cell and then:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return self.sectionSizeForIndex(section)
}

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    let cell = self.collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "cellID", forIndexPath: indexPath) as! FooCollectionViewCell
    cell.setupLabelWithText("Lorem ipsum")
    return cell
}

For debugging reasons, I show both the borders of the cell and the borders of the inner label, with different colors.

Here's what happens: The cell got the right frame, but the inner label seems not update constraints according to its parent view (cell).

On "Debug View Hierarchy", I see that also cell.contentView doesn't update itself -> I suppose that is this the reason why the label doesn't update.

In cell awake from nib:

override func awakeFromNib() {
    super.awakeFromNib()
    self.label.numberOfLines = 0
    self.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    self.translatesAutoresizingMaskIntoConstraints = true
}
Wouter
  • 1,568
  • 7
  • 28
  • 35
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146

2 Answers2

0

add this inside viewForSuplementary

 switch kind {
    case UICollectionElementKindSectionHeader:
    let cell = self.collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "cellID", forIndexPath: indexPath) as! FooCollectionViewCell
cell.setupLabelWithText("Lorem ipsum")
return cell
    default: break

 }

this will let you collectionView know that you actually are using a header and not a footer or other type of view kind

anho
  • 1,705
  • 2
  • 20
  • 38
  • Not my case: I register header cell only. Indeed printing kind I get "UICollectionElementKindSectionHeader" only. – Luca Davanzo Jan 16 '17 at 10:09
  • i see... then is you label connected to the outlet in the code ? and as well do you properly set the value for it ? (can i see the code for that - the setupLabelWithText function) also you don't need to use override func awakeFromNib() in your case – anho Jan 16 '17 at 10:15
0

The problem may be that you are using an instance of UICollectionViewCell for a supplementary view instead of the usual custom subclass of UICollectionReusableView.

UICollectionReusableView does not have a contentView property, unlike UICollectionViewCell. The latter creates its own view for that property by manipulating your view hierarchy and constraints. It manages the frame of the content view, and gives it a gesture recognizer, amongst other things. The relationship between the cell (which is itself a view) and the content view (as a subview of the cell) is somewhat opaque and has led to developer grief in previous versions of iOS.

There is nothing in the documentation expressly forbidding the use of UICollectionViewCell as a supplementary view. But there are implicit indications that the expected practice is to the contrary.

The purpose of UICollectionViewCell is to "present the main content" of your collection view, or "your main data items". It "presents the content for a single data item". Supplementary views, on the other hand, "are separate from the collection view's cells". "Like cells, these views are provided by the data source object, but their purpose is to enhance the main content of your app."

When configuring supplementary views in a storyboard, the "Collection View Programming Guide for iOS" provides the following guidance:

For supplementary views, drag a Collection Reusable View from the object library and drop it on to your collection view. Set the custom class and the collection reusable view identifier of your view to appropriate values.

There is no express guidance about configuring supplementary views programmatically, but there is also no reason to think the expected practice would be any different.

In testing your scenario (iOS 10.2 and iOS 9.3, Xcode 8), I could not reproduce the problem. I found that the frames of the content view and label were both set correctly.

But the easiest solution to your problem is likely to be to adopt the expected practice:

  1. Change FooCollectionViewCell (and any associated nib file) from a subclass of UICollectionViewCell to a subclass of UICollectionReusableView.

  2. Remove the autoresizing mask lines from awakeFromNib.

Community
  • 1
  • 1
jamesk
  • 3,807
  • 21
  • 38