Cell size not updating after changing flow layout's itemSize
It seems it's a bug of UICollectionViewFlowLayout,
I have to call prepare() before invalidateLayout()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else
{
return
}
layout.itemSize = cellSize
layout.minimumLineSpacing = space
layout.prepare() // <-- call prepare before invalidateLayout
layout.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.
Cannot change custom UICollectionViewCell in Storyboard
I ran into the same problem.
Turned out that the item size was defined by the contained flow layout. So editing the item size in the flow layout, and the parent collection view had its item size changed accordingly.
How to set UICollectionViewCell Width and Height programmatically
Use this method to set custom cell height width.
Make sure to add this protocols
UICollectionViewDelegate
UICollectionViewDataSource
UICollectionViewDelegateFlowLayout
If you are using swift 5 or xcode 11 and later you need to set Estimate Size
to none
using storyboard in order to make it work properly. If you will not set that than below code will not work as expected.
Swift 4 or Later
extension YourViewController: UICollectionViewDelegate {
//Write Delegate Code Here
}
extension YourViewController: UICollectionViewDataSource {
//Write DataSource Code Here
}
extension YourViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: screenWidth, height: screenWidth)
}
}
Objective-C
@interface YourViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(CGRectGetWidth(collectionView.frame), (CGRectGetHeight(collectionView.frame)));
}
the behavior of the UICollectionViewFlowLayout is not defined, because the cell width is greater than collectionView width
This happens when your collection view resizes to something less wide (go from landscape to portrait mode, for example), and the cell becomes too large to fit.
Why is the cell becoming too large, as the collection view flow layout should be called and return a suitable size ?
collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)
Update to include Swift 4
@objc override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{ ... }
This is because this function is not called, or at least not straight away.
What happens is that your collection view flow layout subclass does not override the shouldInvalidateLayoutForBoundsChange
function, which returns false
by default.
When this method returns false, the collection view first tries to go with the current cell size, detects a problem (which logs the warning) and then calls the flow layout to resize the cell.
This means 2 things :
1 - The warning in itself is not harmful
2 - You can get rid of it by simply overriding the shouldInvalidateLayoutForBoundsChange
function to return true.
In that case, the flow layout will always be called when the collection view bounds change.
Related Topics
Why Is the Tab Bar Disappearing
How to Check How Long Ago Was the App Last Opened
Find If User Is in a Call or Not
Why It Shows "Ld: Framework Not Found Bolts"
Airpods Not Working as an Input Source for Voice Recorder App
Swift - Json Error: the Data Couldn'T Be Read Because It Isn'T in the Correct Format
Swift: Reload a View Controller
Redirect to Application If Installed, Otherwise to App Store
Swift Parse Json - the Data Couldn'T Be Read Because It Isn'T in the Correct Format
What's the Difference Between the Atomic and Nonatomic Attributes
Set the Maximum Character Length of a Uitextfield
How to Hide Uinavigationbar 1Px Bottom Line
Generate .Pem File Used to Set Up Apple Push Notifications