【发布时间】:2021-02-12 04:08:36
【问题描述】:
我的目标是创建如下所示的布局:
我知道如何创建这些自定义 UICollectionViewCells,但我在布局上遇到了麻烦。所有显示的单元格的宽度都不同,因此可以有,例如:第一行有四个,第二行有两个,最后一个在第三行。这只是一种可能的配置,根据标签的宽度,还有更多配置(包括图像上显示的配置)。
我正在以编程方式构建所有内容。我也觉得使用UICollectionView 是最好的选择,但我愿意接受任何建议。
提前致谢!
我已经尝试过的:
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: TagLayout()
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
private func setupCollectionView() {
collectionView.backgroundColor = .systemGray5
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(SubjectCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
//adding a view to subview and constraining it programmatically using Stevia
}
extension ProfileVC: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? SubjectCollectionViewCell else { return UICollectionViewCell() }
cell.data = SubjectTagData(emoji: "????????", subjectName: "Item I")
return cell
}
}
【问题讨论】:
标签: ios swift uicollectionview uikit custom-collection