0

I created an UICollectionViewCell (.xib) and I just wanted to add a label and some simple constraints there, as such:

Simple Constraints

However, doing this messes up everything. No matter the size I chose on UICollectionViewDelegateFlowLayout, the cell will always use the size of the label! What am I doing wrong here? Am I supposed to not use constraints on CollectionViewItems and instead translatesMaskIntoConstraints = true? Thanks!

edit: Here's what I expected: Expected Result

And here's what I actually have (as soon as I add the constraints): Result with constraints

edit2: Fixed! By changing the label's content hugging priority to 10 (for example, or lower). But...why exactly if there are no other constraints in the view? If somebody can answer it, I would be very grateful, cause I could just answer this question and give this solution but I can't explain why exactly it fixed it, so it's not a proper answer anyway.

Mykod
  • 419
  • 4
  • 14

1 Answers1

1

You have 2 ways: you can delegate to a cell to calculate own height or you can set fixed size for height.

If you want the first case, please, read other related answers

If you want fixed size, you should make a fixed height on the label. Label is calculating own size based on contentSize, so your constraints are failed in the matter of fixed size of your cell

Iliya Kisliy
  • 201
  • 1
  • 5
  • Hello. Thanks for your answer. Although, I'm not quite sure you understood exactly my issue. Could you check the updated question, please? Thanks! – Mykod Nov 10 '19 at 14:30
  • @Mykod hello! Hugging is working because then your cell size is more prioritized in sizing. See the nature of your problem: UILabel has its own size based on content, so due to its size and its offsets of the edges, it's trying to size your cell. What you can do: align not by leading/trailing/bottom/top, but constraint its to center of cell – Iliya Kisliy Nov 11 '19 at 13:48