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
Whats the Swift Animate Withduration Syntax
Swiftui Hierarchical Picker With Dynamic Data Crashes
Reasons to Include Function in Protocol Definition VS. Only Defining It in the Extension
How to Draw Text in PDF Context in Swift
Swift Semantics Regarding Dictionary Access
Binary Operator '/' Cannot Be Applied to Two 'Double' Operands
Highlight a Specific Part of the Text in Swiftui
Swift Make Method Parameter Mutable
Safariviewcontroller: How to Grab Oauth Token from Url
Where Is Info.Plist in Xcode 13? (Missing, Not Inside Project Navigator)
Is There a Method to Blur a Background in Swiftui
Swiftui. How to Change the Placeholder Color of the Textfield
Arkit - Get Current Position of Arcamera in a Scene
How to Add an Event in the Device Calendar Using Swift
How to Save Data from Cloud Firestore to a Variable in Swift
Try, Try! & Try? What's the Difference, and When to Use Each