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.
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 }}
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".
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.
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.
A layout object that organizes items into a grid with optional header and footer views for each section.
Add this function:
override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() myCollection.collectionViewLayout.invalidateLayout() }
When you change the orientation, this function would be called.
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() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With