![](/img/trans.png)
[英]Is there a way to use Visual Format Language to constrain a view to the bottom of a navigation bar?
[英]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
调整它,它不会给我基线的标签(只是选择Top
, Bottom
,和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.