Implementing collectionViewContentSize

A collection view uses the collectionViewContentSize property from its layout to figure out the size of its contents. This property is especially important because it is used to configure and display the scrolling indicators for the collection view. It also provides the collection view with information about the direction in which scrolling should be enabled.

Implementing this property uses the number of rows and columns in the collection view. It also takes the item size and item spacing into account to come up with the size of all of its contents together. Add the following implementation for collectionViewContentSize:

override var collectionViewContentSize: CGSize {
  let width = CGFloat(numberOfColumns) * itemSize.width + CGFloat(numberOfColumns - 1) * itemSpacing
  let height = CGFloat(numberOfRows) * itemSize.height + CGFloat(numberOfRows - 1) * itemSpacing

  return CGSize(width: width, height: height)
}

Determining the entire size of the collection view is quite simple now because the prepare() method has already performed all the hard work to set up the layout.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset