Search code examples
iosswiftxcodeuicollectionviewstoryboard

Calling invalidateLayout() or reloadData() to reload a UICollectionView created from storyboard after device rotation


Within a UITableViewController, UITableViewCell, I've a grid with fixed number of static UICollectionViewCells (without spacing) per row.

extension NounsTVC: UICollectionViewDataSource {

    func collectionView( _ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellIds.count
    }

    func collectionView( _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell( withReuseIdentifier: cellIds[indexPath.item], for: indexPath)
        cell.layer.borderColor = UIColor.gray.cgColor
        cell.layer.borderWidth = 0.5
        return cell
    }
}

extension NounsTVC: UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let availableWidth = collectionView.bounds.size.width
        let minimumWidth = floor(availableWidth / cellsPerRow)
        let remainder = availableWidth - minimumWidth * CGFloat(cellsPerRow)

        let columnIndex = indexPath.row % Int(cellsPerRow)
        let cellWidth = CGFloat(columnIndex) < remainder ? minimumWidth + 1 : minimumWidth
        return CGSize(width: cellWidth, height: 45)
    }
}

Works fine.

But, when I rotate the device, it needs to recall these layout methods.

This looks like the answer: Swift: How to refresh UICollectionView layout after rotation of the device

But I do not have my UICollectionView as invalidLayout() because the UICollectionView is created in IB (as i understand), so how do i call invalidateLayout on my UICollectionView?


Solution

  • Two problems were identified in the above situation.

    Firstly, xcode was not creating an IBOutlet in my custom UITableViewCell by dragging a line between the storyboard and the .swift file (apparently a bug in some constellations). The solution was to type the code in manually, and ctrl-drag and drop this in reverse to the correct view in the storyboard.

    @IBOutlet weak var myCollectionView: UICollectionView!
    

    Secondly, because the collection view was instantiated in a custom UITableViewCell, it wasn't possible to use viewWillTransitionToSize:withTransitionCoordinator: or viewWillLayoutSubviews (table view methods). However, UITableViewCell does have the following, where .invalidateLayout() can be called:

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
    {
        super.traitCollectionDidChange(previousTraitCollection)
        guard previousTraitCollection != nil else
        { return }
        myCollectionView.collectionViewLayout.invalidateLayout()
    }