Объясните пожалуйста как вы размещаете CollectionVIew, чтобы ширина была адаптивной и при этом не вылетали ошибки в лог? Нужно чтобы работало на ios11 и выше…
Вот моя коллекция:
class BtnCollection: UICollectionViewController, UICollectionViewDelegateFlowLayout {
public let flowLayout = UICollectionViewFlowLayout()
override func viewDidLoad() {
super.viewDidLoad()
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
flowLayout.scrollDirection = .vertical
collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
collectionView.backgroundColor = .clear
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
flowLayout.invalidateLayout()
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if indexPath.row == 1
{
cell.backgroundColor = .red
}
else
{
cell.backgroundColor = .brown
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellWidth: CGFloat = collectionView.frame.width
let cellHeight: CGFloat = 40
return CGSize(width: cellWidth, height: cellHeight)
}
}
Когда размер окна меняется вылетают такие вот ошибки (поворот экрана или сжатие окна в iPad):
2020-06-15 06:40:27.494289+0300 Asdbet[12843:7018397] The behavior of the UICollectionViewFlowLayout is not defined because:
2020-06-15 06:40:27.494445+0300 Asdbet[12843:7018397] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2020-06-15 06:40:27.495163+0300 Asdbet[12843:7018397] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7f98fca04bb0>, and it is attached to <UICollectionView: 0x7f98fd028200; frame = (0 0; 375 120); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60000053ea60>; animations = { position=<CABasicAnimation: 0x600000b00240>; bounds.origin=<CABasicAnimation: 0x600000b02f20>; bounds.size=<CABasicAnimation: 0x600000b03600>; }; layer = <CALayer: 0x600000b68640>; contentOffset: {0, 0}; contentSize: {812, 120}; adjustedContentInset: {0, 44, 0, 44}; layout: <UICollectionViewFlowLayout: 0x7f98fca04bb0>; dataSource: <Asdbet.BtnCollection: 0x7f98fca04830>>.
2020-06-15 06:40:27.495952+0300 Asdbet[12843:7018397] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.