求VFL语句'V:|[v(>=height)]-0.0@highPriority-|'对应的NSLayoutConstraint实现
Hey there! Let's walk through how to translate that VFL statement into explicit NSLayoutConstraint code, and fill in the missing bottom constraint you started on.
First, let's break down what the VFL string V:|[v(>=height)]-0.0@highPriority-| is doing:
V:specifies we're defining vertical constraints- The leading
|refers to the parent view's top edge, and the trailing|refers to the parent view's bottom edge [v(>=height)]creates a constraint that the viewvhas a height greater than or equal to the value we'll set as the constant-0.0@highPriority-sets a constraint that the space betweenv's bottom edge and the parent's bottom edge is 0, with a high priority (matchingUILayoutPriority.defaultHigh)
Here's the complete code that matches this VFL, including the finished bottom constraint:
// Assume `v` is the target view, and `parentView` is its superview (replace with your actual views) let v = yourTargetView let parentView = v.superview! // Height constraint: matches [v(>=height)] in VFL let heightConstraint = NSLayoutConstraint( item: v, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: height // Replace `height` with your actual minimum height value (you had 0 in your snippet, adjust as needed) ) // Bottom constraint: matches the -0.0@highPriority- part leading to the trailing | let bottomConstraint = NSLayoutConstraint( item: v, attribute: .bottom, relatedBy: .equal, toItem: parentView, attribute: .bottom, multiplier: 1.0, constant: 0.0 ) // Set the high priority for the bottom constraint bottomConstraint.priority = .defaultHigh // Don't forget to activate the constraints! NSLayoutConstraint.activate([heightConstraint, bottomConstraint])
A quick note: In your original snippet, you used self.view! as the item for the height constraint — make sure that's actually the view v referenced in the VFL, otherwise the constraint won't target the right element. Also, remember to set translatesAutoresizingMaskIntoConstraints = false on v before activating these constraints, otherwise Auto Layout will ignore your explicit constraints.
内容的提问来源于stack exchange,提问作者Ramesh

