I have a Collection View with cells of dynamic height (based on a possibly multiline label and a textview inside), which adapts in height perfectly when I span it over multiple lines. However, when the text is just one word or it doesn't cover the entire screen width, the cell is just as wide as needed to be, resulting in cells next to each other instead of underneath each other.
Take a look at the result with different lengths of text inside the textview.
However, I want the cells to be underneath each other, think Instagram or Twitter kind of view. I obviously need to set a width constraint for the cell so it is equal to the entire width of the screen, but I can't seem to find how to do this.
I use FlowLayout with an estimatedItemSize in my AppDelegate:
let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
layout.estimatedItemSize = CGSize(width: 414, height: 150)
I also set constraints for the collectionView in viewDidLoad, so the collectionView takes on the entire width and height of the screen:
collectionView?.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
collectionView?.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
collectionView?.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
collectionView?.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
My constraints for the label and the textview look like this, also allowing the width to take on the size required (so it's not fixed):
// vertical constraints usernameLabel & commentTextView
addConstraintsWithFormat(format: "V:|-16-[v0]-2-[v1]-16-|", views: usernameLabel, commentTextView)
// horizontal constraints usernameLabel
addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: usernameLabel)
// horizontal constraints commentTextView
addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: commentTextView)
All this is, according to me, essential to make this work. However, it seems to me that I have yet to set a width constraint to make this work. I have tried to add a widthAnchor on the collectionView (equal to view.widthAnchor), this didn't work:
collectionView?.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
I have also tried to set the width in the sizeForItemAt method, but this also didn't work:
return CGSize(width: view.frame.width, height: 100)
Now, I am checking the width to see where it goes wrong, but I get the result I would need, I think:
print("collectionView width: ", collectionView?.frame.size.width)
print("collectionView height: ", collectionView?.frame.size.height)
print("screen width: ", UIScreen.main.bounds.width)
print("screen height: ", UIScreen.main.bounds.height)
Results in:
collectionView width: Optional(414.0)
collectionView height: Optional(736.0)
screen width: 414.0
screen height: 736.0
I am out of ideas on how to move forward. All articles or posts I have gone through related to this issue either explicitly calculate the height or use the two solutions I have mentioned above.
Any ideas on what I am doing wrong?