ios – Swift:无效参数不满足:约束

我收到了错误:

Invalid parameter not satisfying: [constraint isKindOfClass:[NSLayoutConstraint class]]

对于我用Swift编写的自动布局约束代码:

let d:NSDictionary = ["scrollView": scrollView]
    let hc:NSArray = [NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|",options: NSLayoutFormatOptions.allZeros,metrics: nil,views: d)]
    let vc:NSArray = [NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|",views: d)]
    view.addConstraints(hc)
    view.addConstraints(vc)

有谁知道这个错误试图告诉我什么?据我所知,参数是NSArray类型,其中包含NSLayoutConstraint对象.我怀疑我必须明确地将数组定义为NSArray,但这并没有解决它.

解决方法

NSLayoutConstraint.constraintsWithVisualFormat已经返回一个NSArray,因此您不需要将其包装在另一个数组中.所以尝试:
let d:NSDictionary = ["scrollView": scrollView]
let hc:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|",views: d)
let vc:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|",views: d)
view.addConstraints(hc)
view.addConstraints(vc)

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...