I have a very simple UICollectionView embedded in a ViewController.
The UICollectionView uses auto-layout (defined in IB) to take the whole space in the ViewController.
My UICollectionViewCell have all the same size and they take the whole space of the UICollectionView because I want only one cell to be displayed in the ViewController. The UICollectionViewCell displays only an UIImage (which is embedded in a UIScrollView for zooming).
It works fine except when the device rotate. There's an error message:
The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. The relevant UICollectionViewFlowLayout instance is , and it is attached to ; animations = { bounds.origin=; bounds.size=; position=; }; layer = ; contentOffset: {0, 0}; contentSize: {4875, 323}> collection view layout: .
That's pretty clear, I need to resize my UICollectionViewCell to fit the new height / width. After the rotation the UICollectionViewCell display with the appropriate height / width but with a poor animation.
Here's the code:
@IBOutlet weak var theCollectionView: UICollectionView!
@IBOutlet weak var theFlowLayout: UICollectionViewFlowLayout!
override func viewDidLoad() {
super.viewDidLoad()
//theCollectionView.delegate = self
theCollectionView.dataSource = self
automaticallyAdjustsScrollViewInsets = false
theFlowLayout.itemSize = theCollectionView.frame.size
theFlowLayout.sectionInset = UIEdgeInsetsMake(0,0,0,0)
theFlowLayout.minimumLineSpacing = 0.0
theCollectionView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
print("\(#function) \(view.frame.size)")
coordinator.animate(alongsideTransition: nil, completion: {
_ in
self.theFlowLayout.itemSize = self.theCollectionView.frame.size
})
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
print("\(#function) \(view.frame.size)")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("\(#function) \(view.frame.size)")
}
The question is, how and when perform the UICollectionViewCell change to avoid the warning and get the smooth animation?
When the device rotation happens, I can see the following: viewWillTransition(to:with:) (375.0, 667.0) viewWillLayoutSubviews() (667.0, 375.0) viewDidLayoutSubviews() (667.0, 375.0)
The error is displayed just after the viewDidLayoutSubviews(). I have tried to call theFlowLayout.invalidateLayout() in viewDidLayoutSubviews() but it doesn't change the issue.
I have read others similar questions but I could not find my answer :-(
Thanks for your help,
Sebastien