为什么我们需要 invalidateLayout?

Nin*_*nja 5 ios swift ios-autolayout

我尝试实现旋转调整,因此在教程中作者在设置新首选项之前使用了 invalidatelayout 功能。但实际上所有工作都可以在没有 invalidateLayout 的情况下工作,但我之前在另一种情况下尝试过并得到相同的“无”输出。

   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()
    }

}
Run Code Online (Sandbox Code Playgroud)

那么为什么我们需要 invalidateLayout 呢?(我阅读了文档,但似乎这个功能有点......它似乎什么都不做......我不知道......也许是多余的?)