#swift addConstraint 添加约束报错问题
view.addConstraint(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|", options: [], metrics: nil, views: dicM))
报错内容:
Cannot convert value of type '[NSLayoutConstraint]' to expected argument type 'NSLayoutConstraint'
image.png
报错原因为类型不匹配,返回类型应为constraints数组
但是添加 [] 类型扔报错
此处解决方案如下:
多约束应使用addConstants而非addConstant
修改举例如下:
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|",options: [],metrics: nil,views: ["view": self.view]))
参考api方法:
@available(iOS 6.0, *)
open func addConstraint(_ constraint: NSLayoutConstraint)
@available(iOS 6.0, *)
open func addConstraints(_ constraints: [NSLayoutConstraint])
网友评论