Swift: How to refresh UICollectionView layout after rotation of the device

后端 未结 14 861
闹比i
闹比i 2020-12-01 02:38

I used UICollectionView (flowlayout) to build a simple layout. the width for each cell is set to the width of screen using self.view.frame.width

but whe

相关标签:
14条回答
  • 2020-12-01 03:20

    The better option is to call invalidateLayout() instead of reloadData() because it will not force recreation of the cells, so performance will be slightly better:

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews() 
        myCollection.collectionViewLayout.invalidateLayout()
    }
    
    0 讨论(0)
  • 2020-12-01 03:20

    It works for me. And this is code in Objective-C:

    - (void)viewDidLayoutSubviews {
      [super viewDidLayoutSubviews];
      [collectionView.collectionViewLayout invalidateLayout];
    }
    
    0 讨论(0)
提交回复
热议问题