ios - 如何在 VFL 中编写 topLayoutGuide 约束

标签 ios swift autolayout

我正在学习通过代码在 VFL 中编写约束

我已将 View 水平居中,如下所示:

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

但我想知道如何将约束写入 topLayoutGuide 在 Storyboard中看起来像那样 enter image description here

我最终得到了这样的结果,但它不起作用;(

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

最佳答案

您可以定义如下:

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

关于ios - 如何在 VFL 中编写 topLayoutGuide 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862959/

相关文章:

swift - 使用 map 快速修改 Realm 值

ios - 在 UITableView 中使用动态标签宽度管理自动标题高度

ios - 自动布局 View 层次结构

ios - AVAssetImageGenerator 有时会从 2 个连续帧返回相同的图像

ios - 如何创建自己处理协议(protocol)的 UITableViewController 子类

swift - 在元组中保存响应时如何忽略 async let 抛出错误?

ios - 为Xcode 11中的旧源代码启用自动布局

ios - Swift:意外执行 segue

ios - UITableview 在滚动之前需要多次尝试滚动

ios - NavigationController nil with Material