Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

ios - NSLayoutConstraint style for VFL

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

What will be the constraint (NSLayoutConstraint style) for above VFL.

Perhaps its considering view height with greaterThanEqual & bottom constraint with UILayoutPriority.defaultHigh.

Something i used -

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]) 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Initial Set up for Answer:

let parentView = self.view!

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

For given VFL:

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


1. VFL Implementation:

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 Implementation:

The above VFL's NSLayoutConstraint equivalent is:

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)

NOTE: The given VFL in your question only provides the childViews y position and height.
For width, add the constraints accordingly.

VFL Example for width would be:

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

Ref:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...