VFL 的 NSLayoutConstraint 样式 [英] NSLayoutConstraint style for VFL

查看:62
本文介绍了VFL 的 NSLayoutConstraint 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

上面 VFL 的约束是什么(NSLayoutConstraint 样式).

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

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

Perhaps its considering view height with greaterThanEqual & bottom constraint with 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]) 

推荐答案

针对答案的初始设置:

let parentView = self.view!

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


对于给定的 VFL:


For given VFL:

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

"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 仅提供 childView 的 y 位置和高度.
对于宽度,相应地添加约束.


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

宽度的 VFL 示例为:

VFL Example for width would be:

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


参考:


Ref:

这篇关于VFL 的 NSLayoutConstraint 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆