2016-04-26 35 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里面,无论如何你的答案是正确的;) –