Uicollectionview - Resizing Cells on Device Rotate - Swift

UICollectionView - resizing cells on device rotate - Swift

You might use viewWillLayoutSubviews. This question should be helpful but this is bassically called whenever the view controller views is about to layout its subviews.

So your code will look like this:

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}

if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
//here you can do the logic for the cell size if phone is in landscape
} else {
//logic if not landscape
}

flowLayout.invalidateLayout()
}

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

Add this function:

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

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

How to auto-resize UICollectionView when device rotates?

See my solution.

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 100, height: 100)
let controller = UICollectionViewController(collectionViewLayout: layout))

In iOS8+ the function is not override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {} anymore.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if let layout = self.collectionView?.collectionViewLayout as? UICollectionViewFlowLayout{
layout.itemSize = CGSize(width: 200, height: 200)
}
}

This works for me. Try it.

Resizing UICollectionViewCells on rotation changes currently visible cells

You should handle content offset of your UICollectionView, so in your view controller try to override viewWillTransitionToSize function from UIContentContainer protocol, like this:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

let offset = yourCollectionView?.contentOffset;
let width = yourCollectionView?.bounds.size.width;

let index = round(offset!.x / width!);
let newOffset = CGPointMake(index * size.width, offset!.y);

yourCollectionView?.setContentOffset(newOffset, animated: false)

coordinator.animateAlongsideTransition({ (context) in
yourCollectionView?.reloadData()
yourCollectionView?.setContentOffset(newOffset, animated: false)
}, completion: nil)
}

How to fix item width in UIcollectionView when moving from portrait to landscape?

You can override viewWillTransition method and reload so that collectionView recalculate the size.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

self.collectionView?.reloadData()
}


Related Topics



Leave a reply



Submit