在我使用的集合视图中UICollectionViewCompositionalLayout
,当视图宽度较小时,我希望项目大小为 85x85,否则为 100x100。但是这是在创建布局时指定的,那么当屏幕宽度发生变化时如何更改布局大小,例如旋转 iPhone 或更改 iPad 上的拆分视图配置?
collectionView.collectionViewLayout = {
let size = CGFloat(view.bounds.width < 375 ? 85 : 100)
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(size), heightDimension: .absolute(size))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(size))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
return UICollectionViewCompositionalLayout(section: section)
}()