要运行示例项目,请克隆仓库,并在首先在Example目录中运行pod install
。
Pin旨在简化AutoLayout代码。它的目的是比NSLayoutConstraint代码更容易阅读。以下是匹配视图宽度与其父视图宽度的典型AutoLayout代码
let view = UIView()
self.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
let constraint = NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0.0)
view.addConstraint(constraint)
以下是使用Pin的相同示例
let view = UIView()
self.addSubview(view)
Pin(view: view).width().constrain()
请注意,使用Pin不需要'view.translatesAutoresizingMaskIntoConstraints = false'。
2. 您设置的属性,需要在两个视图上匹配(即视图的宽度等于父视图的宽度)。
4. 加权因子 = 1.0 和常数 = 0.0。
let otherView = UIView()
self.addSubview(otherView)
...
let view = UIView()
self.addSubview(view)
Pin(view: view).top().to(otherView).bottom().constrain()
如果这些假设不正确,您仍然可以使用Pin设置额外的属性。
如果您想一个视图的顶部与另一个视图的底部相等
Pin(view: view).width().multiplier(0.5).constant(10.0).constrain()
您可以这样读取此约束:Pin -> view.top -> to -> otherView.bottom。
如果您想设置加权和常数,可以使用以下方式
Pin(view: view).width(50.0).constrain()
Pin(view: view).height(100.0).constrain()
宽度和高度固定约束
Pin(view: view).width(multiplyBy: 0.5).relation(.LessThanOrEqual).constrain()
更改关系
Pin(view: view).width(multiplyBy: 0.5).relation(.LessThanOrEqual).constrain()
Pin(view: view).width(200.0).priority(750).constrain()
Pin假定约束关系是NSLayoutRelation.Equal。
Pin 可以通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中
pod "Pin"
Stuart Lynch, [email protected]
Pin 可在 MIT 许可下获得。有关更多信息,请参阅 LICENSE 文件。