如何在VFL中编写topLayoutGuide约束

时间:2016-04-26 10:59:11

标签: ios swift autolayout

我正在学习用代码

在VFL中编写约束

我把我的观点水平地集中在那里:

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

但我想知道如何将约束写入topLayoutGuide 那样的故事板 autoresizingMask

我最终得到了类似的东西,但它不起作用;(

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

1 个答案:

答案 0 :(得分: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