ConstraintLib 2.0.0

ConstraintLib 2.0.0

测试已测试
语言语言 SwiftSwift
许可 自定义
发布上次发布2017年11月
Swift版本Swift版本3.0
SPM支持SPM

Stefan Renne维护。



  • 作者:
  • Stefan Renne

ConstraintLib

Travis Badge
CocoaPods Version Badge
Supported Platforms Badge
Percentage Documented Badge
License Badge

Swift 库,通过添加以下特性简化了与 Xcode 约束的交互

  • 使用可理解的功能添加 Swift 约束
  • 从代码中查找约束
  • 动画 Swift 约束更改
  • 从其父视图移除约束

此库现在需要 Swift 4。

背景信息

我喜欢制作自定义视图和自定义动画。但是每次我这样做,我发现自己都在写大量的约束代码来管理子视图、创建约束属性和执行动画。使用这个分类/库,我成功地让我的自定义视图变得简单得多。

用法

本模块包含对 UIViewUIViewControllerNSLayoutConstraint 的扩展。

示例 1:创建具有 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)

示例 2:创建不具有 NSLayoutConstraint 属性的视图

如果你不需要约束属性,则也可以使用此语法。

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]

示例 3:创建一个视图并提供常量值

或者,也可以在固定命令中提供值。

完整格式

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]

示例 4:创建一个与另一个视图有关系的视图

为了创建一个视图 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])

示例 5:固定到顶部和/或底部 UILayoutGuide

要将视图固定到 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]

示例 6:默认乘数

默认乘数只能在单行创建器中修改。

let newView1 = UIView()
self.view.addSubview(newView1)
let topConstraint = newView1.pin(.top, constant: 50.0, multiplier: 2.0)

初始化后,可以通过使用乘数属性来修改乘数。

newView1.constraint(.top)?.multiplier = 1.5

示例 7:从视图中找到一个 NSLayoutConstraint

可以通过以下方式找到约束:

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))

基于 topLayoutGuidebottomLayoutGuide 的 NSLayoutConstraint 的查找尚未可用。

示例 8:动画约束更改

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
})

示例 9:移除约束

移除左边或领导约束

view1.constraint(.left)?.remove()

view1的右侧移除到view2的左侧的水平约束

view1.constraint(.rightTo(view2))?.remove()

更多演示?

克隆存储库,打开ConstraintLib.xcodeproj并构建演示项目

Demo Project

开发信息

请在单元测试中记录代码更改,并确保所有测试都是绿色的。

许可

本项目采用Apache-2.0许可证发布。