My goal is to create a layout presented below:
I know how to create these custom UICollectionViewCells, but I trouble with the layout. All of the shown cells differ in width, so there can be, for instance: four in the first row, two in the second, and the last one remaining - in the third. That's just one possible configuration, and there are many more (including one shown on an image) depending on the label's width.
I'm building everything programmatically. Also I feel like using UICollectionView
is the best choice, but I'm open to any suggestions.
Thanks in advance!
What I've already tried:
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
}
}
Use following collectionViewLayout
And implement it as follows