SimplyLayout
基于锚点的 Auto Layout 的 Swift 语法糖
安装
CocoaPod
# Swift 4 with full functions, including `Core`, `Anchor` and `Group`
pod 'SimplyLayout'
# Swift 4 with syntactic sugar only
pod 'SimplyLayout/Core'
# Swift 4 with convenience virtual anchors (CenterAnchor, EdgeAnchor, etc.)
pod 'SimplyLayout/Anchor'
# Swift 4 with constraint grouping functions
pod 'SimplyLayout/Group'
# Swift 3
pod 'SimplyLayout', :git => 'https://github.com/aipeople/SimplyLayout.git', :tag => 'swift3-1.0.1'
使用
操作符
+
,-
-> 设置常量*
-> 设置倍数~
-> 设置优先级
示例
import SimplyLayout
view.widthAnchor == 100
// view.widthAnchor.constraint(equalToConstant: 100).isActive = true
// view.translatesAutoresizingMaskIntoConstraints = false
view.heightAnchor == view.superview!.heightAnchor * 0.25 + 40 ~ .defautHigh
// let constraint = view.heightAnchor.constraint(equalTo: view.superview!.heightAnchor, multiplier: 0.25, constant: 40)
// constraint.priority = .defaultHigh
// constraint.isActive = true
// view.translatesAutoresizingMaskIntoConstraints = false
激活
约束将自动激活。您可以通过设置 SimplyLayout.config.defaultActivation
为 false
来关闭此功能。由于某些原因,您可能想要创建具有不同激活状态的约束。《++》和--,
可以帮您移除默认配置。
示例
SimplyLayout.config.defaultActivation = true
view.widthAnchor == 100
// view.widthAnchor.constraint(equalToConstant: 100).isActive = true
view.widthAnchor == --100
// view.widthAnchor.constraint(equalToConstant: 100)
SimplyLayout.config.defaultActivation = false
view.heightAnchor == view.superview!.heightAnchor * 0.25
// let constraint = view.heightAnchor.constraint(equalTo: view.superview!.heightAnchor, multiplier: 0.25)
view.heightAnchor == ++view.superview!.heightAnchor * 0.25
// let constraint = view.heightAnchor.constraint(equalTo: view.superview!.heightAnchor, multiplier: 0.25)
// constraint.isActive = true
访问约束
可以通过使用 =
运算符轻松访问新创建的约束。
示例
let constraint = view.heightAnchor == view.superview!.heightAnchor * 0.25
虚拟锚点
虚拟锚点不是真实的 NSLayoutAnchor
。这些锚点只是为了帮助您更容易地创建约束。
中心锚点
view.centerAnchor == view.superview!.centerAnchor + CGPoint(x: 10, y: 10)
// view.centerXAnchor == view.superview!.centerXAnchor + 10
// view.centerYAnchor == view.superview!.centerYAnchor + 10
边缘锚点
view.edgeAnchor == view.superview!.edgeAnchor
// view.topAnchor == view.superview!.topAnchor
// view.leadingAnchor == view.superview!.leadingAnchor
// view.bottomAnchor == view.superview!.bottomAnchor
// view.trailingAnchor == view.superview!.trailingAnchor
view.edgeAnchor == view.superview!.edgeAnchor.insetBy(top: 10, left: 20, bottom: 10, right: 20)
// view.edgeAnchor == view.superview!.edgeAnchor + UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
// -----------------------(equals to)------------------------
// view.topAnchor == view.superview!.topAnchor + 10
// view.leadingAnchor == view.superview!.leadingAnchor + 20
// view.bottomAnchor == view.superview!.bottomAnchor - 10
// view.trailingAnchor == view.superview!.trailingAnchor - 20
分组
分组函数需要 SimplyLayout/Group
子模块。它提供了一个方便地将多个约束组合在一起的方法。一组约束由一个 NSLayoutConstraint
数组表示,可以一起激活/关闭。
示例
let boxA = UIView()
let boxB = UIView()
// `verticalConstraintGroup` is an array with NSLayoutConstraint.
// In the following case, it will contains two constraints.
let verticalConstraintGroup = NSLayoutConstraint.group {
boxA.centerXAnchor == boxA.superview!.centerXAnchor
boxB.centerXAnchor == boxB.superview!.centerXAnchor
}
// All the constraints created in the block won't be activated
let horizontalConstraintGroup = NSLayoutConstraint.group(activated: false) {
boxA.centerYAnchor == boxA.superview!.centerYAnchor
boxB.centerYAnchor == boxB.superview!.centerYAnchor
}
// Deactivate all constraints in the group
verticalConstraintGroup.deactivateAll()
// Activate all constraints in the group
horizontalConstraintGroup.activateAll()
配置
postNotificationWhenConstrantCreate
:当使用 SimplyLayout 语法设置约束后,将立即发布名为SimplyLayout.constraintCreatedNotification
的通知。默认值为false
。
可以在 SimplyLayout.config
中设置与约束相关的行为。
- defaultPriority:默认值是
.required
- defaultActivation:默认值是
true