简体   繁体   中英

Is there no way to constrain the baseline of a label to match the bottom edge of another view?

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.

使用以下代码let constraint = NSLayoutConstraint(item:myLabel,attribute:.bottom,relatedBy:relation,toItem:myView,attribute:.bottom,multiplier:1,constant:constant)myLabel.addConstraint(constraint)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM