2016-04-26 49 views
2

我正在學習從代碼編寫約束VFL如何寫一個topLayoutGuide約束在VFL

我有水平居中我認爲這樣的:

let constraintY = NSLayoutConstraint.constraintsWithVisualFormat(
      "H:[superview]-(<=1)-[collaboratorView]", 
      options: NSLayoutFormatOptions.AlignAllCenterY, 
      metrics: nil, 
      views: ["superview":self.assetsView, "collaboratorView":collaboratorView]) 

,但我不知道怎麼寫約束topLayoutGuide 看起來在故事板那樣 enter image description here

我已經結束了這樣的事情,但它不能正常工作;(

let bar = NSLayoutConstraint.constraintsWithVisualFormat(
      "V:|[collaboratorView]", 
      options: [NSLayoutFormatOptions.AlignAllTop], 
      metrics: nil, 
      views: ["collaboratorView":self.collaboratorView]) 

回答

2

您可以如下定義:

let views : [String : AnyObject] = ["collaboratorView": collaboratorView, 
    "topLayoutGuide": topLayoutGuide, 
    "bottomLayoutGuide": bottomLayoutGuide] 

// 2 
var allConstraints = [NSLayoutConstraint]() 

// 3 
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:[collaboratorView]-12-[topLayoutGuide]", 
    options: [], 
    metrics: nil, 
    views: views) 
allConstraints += verticalConstraints 
+0

謝謝了很多,還有一個問題 我總是想,如果這裏的topLayoutGuide什麼:「topLayoutGuide」:topLayoutGuide, 您可以加入實施請問你的答案? –

+0

實施它,你會看到它是什麼。這是UIViewController中的一個定義的屬性, –

+0

哦,我錯過了我在UIView裏面,無論如何你的答案是正確的;) –