Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 when I rotate the device, the cells don't get updated.

enter image description here

I have found a function, which is called upon orientation change :

override func willRotateToInterfaceOrientation(toInterfaceOrientation:    UIInterfaceOrientation, duration: NSTimeInterval) {     //code } 

but I am unable to find a way to update the UICollectionView layout

The main code is here:

class ViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{      @IBOutlet weak var myCollection: UICollectionView!      var numOfItemsInSecOne: Int!     override func viewDidLoad() {         super.viewDidLoad()          numOfItemsInSecOne = 8         // Do any additional setup after loading the view, typically from a nib.     }      override func didReceiveMemoryWarning() {         super.didReceiveMemoryWarning()         // Dispose of any resources that can be recreated.     }      override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {          //print("orientation Changed")     }      func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {         return 1     }      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {         return numOfItemsInSecOne     }      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {         let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellO", forIndexPath: indexPath)          return cell     }      func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{     let itemSize = CGSize(width: self.view.frame.width, height: 100)     return itemSize     }} 
like image 451
Talha Ahmad Khan Avatar asked Apr 27 '16 08:04

Talha Ahmad Khan


People also ask

How do I know that the UICollectionView has been loaded completely?

Simply reload collectionView inside batch updates and then check in the completion block whether it is finished or not with the help of boolean "finish".

How does swift implement UICollectionView?

Select the Main storyboard from the file browser. Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices.

When should I call Invalidatelayout?

You can call this method at any time to update the layout information. This method invalidates the layout of the collection view itself and returns right away. Thus, you can call this method multiple times from the same block of code without triggering multiple layout updates.

What is UICollectionView flow layout?

A layout object that organizes items into a grid with optional header and footer views for each section.


2 Answers

Add this function:

override func viewDidLayoutSubviews() {     super.viewDidLayoutSubviews()      myCollection.collectionViewLayout.invalidateLayout() } 

When you change the orientation, this function would be called.

like image 189
Khuong Avatar answered Oct 07 '22 16:10

Khuong


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() } 
like image 32
kelin Avatar answered Oct 07 '22 16:10

kelin