The cell just needed an UIImageView and UILabel, so I thought it would be pretty straightforward to do, but there are some caveats you need to know. Big thanks to Tom Adriaenssen for pointing them out and not ruining my Sunday afternoon 🙂
This is what the problem looked like
I’ve added the following constraints:
- UILabel: center in superview (horizontal, vertical)
- UIImageView: Pin the Trailing edge to the Leading edge of the UILabel with an offset of -20
- UIImageView: center in superview (vertically)
- As the custom cell gets embedded into the UIPickerView I also pinned all 4 edges of my custom view to the superview with no insets
- I set thesetTranslatesAutoresizingMaskIntoConstraints method to false on my custom cell
The code looked like this
So doing the following resulted in the BAD! version of the image above. What did I do wrong?
First of all, you should _not_ set setTranslatesAutoresizingMaskIntoConstraints to false when you are using AutoLayout within a view which you don’t place (the UIPickerView will take care of this).
Second, don’t pin the edges of the custom cell to the superview! The UIPickerView will figure out the height.
And the most important part I forgot, you should call setNeedsUpdateConstraints() at the end of the init method!
You can check out the project on GitHub to see a working version.