繁体   English   中英

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

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

我有一个自定义的仪表视图。 还有一个标签,显示仪表绘制的数值。 使用AutoLayout约束,我想将标签的baseline与视图的bottom对齐。

当我按ctrl两者之间的阻力而选择底部对齐,然后尝试使用Size Inspector调整它,它不会给我基线的标签(只是选择TopBottom ,和Center Y )。

是否无法约束标签的基线以匹配Storyboard Editor中另一个视图的下边缘?

我可以用直接代码吗? 那个例子会是什么样的?

我确定故事板编辑器似乎并不想直接这样做。 您可以使用以下方式以编程方式执行此操作:

NSLayoutConstraint *constraint = [NSLayoutConstraint
    constraintWithItem: self.myView
    attribute: NSLayoutAttributeBottom
    relatedBy: NSLayoutRelationEqual
    toItem: self.myLabel
    attribute: NSLayoutAttributeBaseline
    multiplier: 1
    constant: 0];
[self.myView.superview addConstraint: constraint];

为了使故事板体验愉快,我使用了从下到下的约束并检查了Placeholder remove at build time选项。

这是不幸的是, secondAttribute财产NSLayoutConstraint是只读的。 否则,您可以创建故事板约束的出口,并在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