在很多地方的代码中,我一直看到这个……
containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":childView]))
containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":childView]))
在我看来,这似乎是多余的。我想知道是否有一种将格式组合成单个字符串的方法;像这样的东西
containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|;V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":childView]))
这样的事情可能吗?
最佳答案
抱歉,您不能使用此功能,但是可以尝试这样的操作
let rr = UIView()
rr.backgroundColor = UIColor.red
self.view.addSubview(rr)
rr.translatesAutoresizingMaskIntoConstraints = false
["H:|-100-[rr]-100-|","V:|-100-[rr]-100-|"].forEach{NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: $0, options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: ["rr":rr]))}
关于ios - 在AutoLayout中,您可以使用Visual Format Language组合水平约束和垂直约束吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49679905/