I wish to create a uicollectionview which looks like this.
The height of the cells will be constant, but the width should automatically change dependant on the length of the label text.
FlowLayout:
func prepareFlowLayout() -> UICollectionViewFlowLayout{
let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
flowLayout.minimumInteritemSpacing = 3
flowLayout.minimumLineSpacing = 3
return flowLayout
}
Label:
let nameLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
//label.backgroundColor = .black
return label
}()
Autolayout inside cell:
backgroundColor = .green
let conView = contentView
layer.cornerRadius = 5
clipsToBounds = true
conView.addSubview(nameLabel)
NSLayoutConstraint.activate([
nameLabel.centerXAnchor.constraint(equalTo: conView.centerXAnchor, constant: 0),
nameLabel.centerYAnchor.constraint(equalTo: conView.centerYAnchor, constant: 0),
nameLabel.heightAnchor.constraint(equalTo: conView.heightAnchor, multiplier: 0.8),
nameLabel.widthAnchor.constraint(equalTo: conView.widthAnchor, multiplier: 0.8)
])
Could anyone send me in the right direction for finding a way which would achieve this?