目前我正在UICollectionView
结合使用 a 和Composotional Layout
. 以下代码产生此结果:
相关方法:
private static func createCompositionalLayout() -> UICollectionViewCompositionalLayout {
let headerItem = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(35)
)
)
let horizontalGroupItem = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(0.95),
heightDimension: .fractionalHeight(1.0)
)
)
let horizontalMainGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(100)
),
subitems: [horizontalGroupItem]
)
horizontalMainGroup.interItemSpacing = .fixed(10)
let group = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(135)
),
subitems: [headerItem, horizontalMainGroup])
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 10
section.orthogonalScrollingBehavior = .continuous
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
所以主要问题是,顶部元素没有占据该部分的整个宽度。相反,它仅限于group
. 有谁知道如何得到想要的结果?:-)
完整示例:
import UIKit
class ViewController: UIViewController {
private let colors: [UIColor] = [.systemTeal, .systemBlue, .systemGray, .systemOrange]
private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: ViewController.createCompositionalLayout())
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.backgroundColor = .systemBackground
collectionView.frame = view.bounds
collectionView.dataSource = self
collectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: MyCollectionViewCell.reuseID)
}
private static func createCompositionalLayout() -> UICollectionViewCompositionalLayout {
let headerItem = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(35)
)
)
let horizontalGroupItem = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(0.95),
heightDimension: .fractionalHeight(1.0)
)
)
let horizontalMainGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(100)
),
subitems: [horizontalGroupItem]
)
horizontalMainGroup.interItemSpacing = .fixed(10)
let group = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(135)
),
subitems: [headerItem, horizontalMainGroup])
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 10
section.orthogonalScrollingBehavior = .continuous
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
}
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.reuseID, for: indexPath) as? MyCollectionViewCell
cell?.backgroundColor = colors[indexPath.item]
cell?.label.text = "\(indexPath.item)"
return cell ?? UICollectionViewCell()
}
}
//MARK: - CollectionViewCell
class MyCollectionViewCell: UICollectionViewCell {
static let reuseID = "myCell"
let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(label)
label.frame = contentView.bounds
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}