-1

"V:|[v(>=height)]-0.0@highPriority-|"

上述 VFL 的约束(NSLayoutConstraint 样式)是什么。

也许它考虑了带有greaterThanEqual& 底部约束的视图高度UILayoutPriority.defaultHigh

我用过的东西-

let heightConstraint = NSLayoutConstraint(item: self.view!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: self.view!, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0)
bottomConstraint.priority = .defaultHigh
NSLayoutConstraint.activate([heightConstraint,bottomConstraint]) 
4

1 回答 1

0

初始设置答案:

let parentView = self.view!

let childView = UIView()
childView.backgroundColor = UIColor.lightGray
childView.translatesAutoresizingMaskIntoConstraints = false
parentView.addSubview(childView)

对于给定的 VFL:

"V:|[v(>=height)]-0.0@highPriority-|"


1. VFL 实现:

let height: CGFloat = 100
let priority: Int = 1000

//VFL (for vertical positioning and height of childView
parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v(>=\(height))]-0.0@\(priority)-|",
                                                         options: [],
                                                         metrics: nil,
                                                         views: ["v" : childView]))

2. NSLayoutConstraint 实现:

上述 VFL 的NSLayoutConstraint等价物是:

let height: CGFloat = 100

//VFL Equivalent: "V:|[v]"
let topConstraint = NSLayoutConstraint(item: childView,
                                       attribute: .top,
                                       relatedBy: .equal,
                                       toItem: parentView,
                                       attribute: .top,
                                       multiplier: 1,
                                       constant: 0)

//VFL Equivalent: "[v(>=height)]"
let heightConstraint = NSLayoutConstraint(item: childView,
                                          attribute: .height,
                                          relatedBy: .greaterThanOrEqual,
                                          toItem: nil,
                                          attribute: .notAnAttribute,
                                          multiplier: 1,
                                          constant: height)

//VFL Equivalent: "[v]|" or "[v]-0.0-|"
let bottomConstraint = NSLayoutConstraint(item: childView,
                                          attribute: .bottom,
                                          relatedBy: .equal,
                                          toItem: parentView,
                                          attribute: .bottom,
                                          multiplier: 1,
                                          constant: 0)
//Adding VFL Equivalent: @priority
bottomConstraint.priority = .defaultHigh

childView.addConstraint(heightConstraint)
parentView.addConstraint(topConstraint)
parentView.addConstraint(bottomConstraint)

注意:您问题中给定的 VFL 仅提供childViewsy 位置和高度。
对于宽度,相应地添加约束。

VFL 宽度示例为:

parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v(100)]",
                                                         options: [],
                                                         metrics: nil,
                                                         views: ["v" : childView]))

参考:

于 2018-05-28T09:26:00.803 回答