I have a custom meter view. And a label that shows the numeric value graphed by the meter. Using AutoLayout constraints, I want to align the baseline
of the label with the bottom
of the view.
When I ctrl drag between the two and choose to align bottoms, and then try to use the Size Inspector
to tune it, it won't give me the option of baseline for the label (just Top
, Bottom
, and Center Y
).
Is there no way to constrain the baseline of a label to match the bottom edge of another view in the Storyboard Editor?
Can I do it in direct code? What would an example of that look like?
I determined that the Storyboard Editor just doesn't seem to want to do this directly. You can do it programmatically with something like this:
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem: self.myView
attribute: NSLayoutAttributeBottom
relatedBy: NSLayoutRelationEqual
toItem: self.myLabel
attribute: NSLayoutAttributeBaseline
multiplier: 1
constant: 0];
[self.myView.superview addConstraint: constraint];
To make the storyboard experience happy, I used a bottom-to-bottom constraint and checked the Placeholder remove at build time
option.
It's unfortunate that the secondAttribute
property of NSLayoutConstraint
is read-only. Otherwise, you could just create an outlet to the storyboard constraint, and just tweak that at viewDidLoad
time.