gpt4 book ai didi

swift - 如何在 Swift 上使用函数 NSLayoutConstraint.constraintsWithVisualFormat?

转载 作者:行者123 更新时间:2023-11-28 09:35:02 24 4
gpt4 key购买 nike

我正在尝试 Swift 并使用布局格式。我使用的 IDE 是 Xcode6-Beta2。

    var viewDictionary:Dictionary = ["myButton": myButton]

var constraintFormat1:AnyObject[]! =
NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myButton]-|",
options : NSLayoutFormatOptions(0),
metrics: nil,
views: viewDictionary)

但我在下面收到错误消息。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'Unable to parse constraint format:
Unable to interpret '|' character, because the related view doesn't have a superview
H:|-[myButton]
^'

它不能在 swift 上工作吗?或者我写错了格式模式?

最佳答案

“|” (竖线)字符指的是您在其内部(直接或间接)声明的 View 的 super View ,在本例中为“myButton”。当您调用 .constraintsWithVisualFormat 时,它会尝试隐式查找您的按钮的 super View ,以便它可以在该 View 和您的按钮之间创建约束。但是,由于您的按钮尚未添加到 View 中,因此它会失败。

关于swift - 如何在 Swift 上使用函数 NSLayoutConstraint.constraintsWithVisualFormat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24290466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com