![](/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.