Moshe Moshe - 2 months ago 12
Objective-C Question

Where should I install my auto layout constraints in a UIView subclass?

I'm subclassing

UIView
to build a custom control. Where do I want to add my layout constraints on subviews?

I've added them in init, but I don't think that's the appropriate place to do anything beyond initialization. I don't want to do in
layoutSubviews
because I'm concerned about subsequent calls recreating the constraints.

What's the right place for this kind of thing?

Answer

There are lots of options here. I think both initWithFrame: and layoutSubviews (with appropriate checks to avoid double-adding) could work, but updateConstraints is the canonical place to install constraints on subviews. You'd also need to avoid double-adding there.