测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可 | 自定义 |
发布上次发布 | 2017年11月 |
Swift版本Swift版本 | 3.0 |
SPM支持SPM | ✗ |
由Stefan Renne维护。
Swift 库,通过添加以下特性简化了与 Xcode 约束的交互
此库现在需要 Swift 4。
我喜欢制作自定义视图和自定义动画。但是每次我这样做,我发现自己都在写大量的约束代码来管理子视图、创建约束属性和执行动画。使用这个分类/库,我成功地让我的自定义视图变得简单得多。
本模块包含对 UIView
、UIViewController
和 NSLayoutConstraint
的扩展。
当一个视图有一个框架时,你可以很容易地将边缘与它的父视图对齐。
总是确保你在固定约束之前将视图添加到父视图中。
let newView = UIView(frame: CGRect(x: 50.0, y: 50.0, width: 100.0, height: 100.0))
self.view.addSubview(newView)
let topConstraint = newView.pin(.top)
let leftConstraint = newView.pin(.left)
let heightConstraint = newView.pin(.height)
let widthConstraint = newView.pin(.width)
如果你不需要约束属性,则也可以使用此语法。
let newView = UIView(frame: CGRect(x: 50.0, y: 50.0, width: 100.0, height: 100.0))
self.view.addSubview(newView)
newView.pin([.top, .left, .height, .width]
或者,也可以在固定命令中提供值。
完整格式
let newView1 = UIView()
self.view.addSubview(newView1)
let topConstraint = newView1.pin(.top, constant: 50.0)
let leftConstraint = newView1.pin(.left, constant: 50.0)
let heightConstraint = newView1.pin(.height, constant: 100.0)
let widthConstraint = newView1.pin(.width, constant: 100.0)
紧凑格式
let newView2 = UIView()
self.view.addSubview(newView2)
newView2.pin([.top: 50.0, .left: 50.0, .height: 100.0, .width: 100.0]
为了创建一个视图 newView2
,其中它的顶部将与视图 newView1
的底部相隔 30 像素。
let newView1 = UIView()
self.view.addSubview(newView1)
newView1.pin([.height, .width, .centerX, .centerY])
let newView2 = UIView()
self.view.addSubview(newView2)
newView2.pin([.left: 100.0, .right: 100.0, .height: 30.0, .topTo(newView1): 30.0])
要将视图固定到 ViewController 的布局指南上,允许视图始终固定到状态栏,甚至到来电状态栏。使用
let topLayoutGuideConstraint = newView1.pin(.topLayoutGuide, constant: 0.0)
let bottomLayoutGuideConstraint = newView1.pin(.bottomLayoutGuide, constant: 0.0)
或者当然其他的格式。
let topLayoutGuideConstraint = newView1.pin(.topLayoutGuide)
newView1.pin([.topLayoutGuide, .bottomLayoutGuide, .left, .right]
newView1.pin([.topLayoutGuide: 0.0, .bottomLayoutGuide: 0.0, .left: 0.0, .right: 0.0]
默认乘数只能在单行创建器中修改。
let newView1 = UIView()
self.view.addSubview(newView1)
let topConstraint = newView1.pin(.top, constant: 50.0, multiplier: 2.0)
初始化后,可以通过使用乘数属性来修改乘数。
newView1.constraint(.top)?.multiplier = 1.5
可以通过以下方式找到约束:
let leftConstraint = view1.constraint(.left)
let rightConstraint = view1.constraint(.right)
let topConstraint = view1.constraint(.top)
let bottomConstraint = view1.constraint(.bottom)
let widthConstraint = view1.constraint(.width)
let heightConstraint = view1.constraint(.height)
let centerXConstraint = view1.constraint(.centerX)
let centerYConstraint = view1.constraint(.centerY)
let leftToView2Constraint = view1.constraint(.leftTo(view2))
let rightToView2Constraint = view1.constraint(.rightTo(view2))
let topToView2Constraint = view1.constraint(.topTo(view2))
let bottomToView2Constraint = view1.constraint(.bottomTo(view2))
let equalwidthToView2Constraint = view1.constraint(.equalWidth(view2))
let equalHeightToView2Constraint = view1.constraint(.equalHeight(view2))
基于 topLayoutGuide
或 bottomLayoutGuide
的 NSLayoutConstraint 的查找尚未可用。
animateConstraints 功能类似于 UIView.animate(withDuration: TimeInterval, animations: Swift(Void))
。
只有一个例外,即 animateConstraints 必须在最高的 UIView 上调用,为了更容易,此函数也已被添加到 UIViewController
的扩展中。
旁边有一个用于常规动画的可选块,一个用于约束动画的块,还有一个可选的完成块。
animateConstraints(duration: 1.0, constraints: {
/* Only contraint changes will be animated in this block */
view1?.constraint(.left)?.constant = 150.0
}, animations: {
/* Other changes will be animated from this block */
view1?.backgroundColor = UIColor.blue
})
移除左边或领导约束
view1.constraint(.left)?.remove()
从view1
的右侧移除到view2
的左侧的水平约束
view1.constraint(.rightTo(view2))?.remove()
克隆存储库,打开ConstraintLib.xcodeproj
并构建演示项目
请在单元测试中记录代码更改,并确保所有测试都是绿色的。
本项目采用Apache-2.0许可证发布。