Why do we need invalidateLayout?

2019-06-25 09:36发布

问题:

i tried to implement rotation adjustments so in tutorial author was using invalidatelayout function before setting new preferences. but all actually works without invalidateLayout, and yet i tried it in another circumstance before and got the same "nothing" output.

   override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {

    collectionView.collectionViewLayout.invalidateLayout()

    let indexPath = IndexPath(item: pageControl.currentPage, section: 0)
    //scroll to indexPath after the rotation is going
    DispatchQueue.main.async {
        self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
        self.collectionView.reloadData()
    }

}

So why do we need invalidateLayout? (and i read docs but it seems that this function is somewhat..it seems do nothing... i don't know...maybe redundant?)