如下图所示,我有一个 UICollectionViewCell,其中包含一个具有固定高度图像的 UIImageView。在 UIImageView 下方有一个垂直的 UIStackView,其中包含所有标签和带有标题、价格的子堆栈视图,例如
导致图像的 2 列网格的组合布局部分是这样的:
func createSection() -> NSCollectionLayoutSection {
// Layout configuration values
let cellHeight = CGFloat(205.0)
let sectionTopSpacing = CGFloat(12.0)
let sectionLeadingSpacing = CGFloat(12.0)
let sectionTrailingSpacing = CGFloat(12.0)
let sectionBottomSpacing = CGFloat(12.0)
let numberOfColumns = UIDevice.current.userInterfaceIdiom == .pad ? 4 : 2
let interItemSpacing = CGFloat(8.0)
let interGroupSpacing = CGFloat(8.0)
// Item
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(cellHeight))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
// Group
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(cellHeight))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,
subitem: item,
count: numberOfColumns)
group.interItemSpacing = .fixed(interItemSpacing)
// Section
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = interGroupSpacing
section.contentInsets = NSDirectionalEdgeInsets(top: sectionTopSpacing,
leading: sectionLeadingSpacing,
bottom: sectionBottomSpacing,
trailing: sectionTrailingSpacing)
return section
}
我已经为垂直堆栈视图及其标签尝试了几种抗压性和拥抱优先级值。
任何想法如何根据该组的最高项目使同一组的项目具有相同的高度都将非常受欢迎。
非常感谢您!