简体   繁体   English

是否无法约束标签的基线以匹配另一个视图的底边?

[英]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. 使用AutoLayout约束,我想将标签的baseline与视图的bottom对齐。

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 ). 当我按ctrl两者之间的阻力而选择底部对齐,然后尝试使用Size Inspector调整它,它不会给我基线的标签(只是选择TopBottom ,和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? 是否无法约束标签的基线以匹配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. 为了使故事板体验愉快,我使用了从下到下的约束并检查了Placeholder remove at build time选项。

It's unfortunate that the secondAttribute property of NSLayoutConstraint is read-only. 这是不幸的是, secondAttribute财产NSLayoutConstraint是只读的。 Otherwise, you could just create an outlet to the storyboard constraint, and just tweak that at viewDidLoad time. 否则,您可以创建故事板约束的出口,并在viewDidLoad时调整它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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