又一个适用于 iOS 的编程式约束库。ClingConstraints 的重点是拥有干净、易于阅读且功能强大的约束创建。
例如,thisView.copy(.height, of: thatView)
功能
translatesAutoresizingMaskIntoConstraints
安装
-
安装 CocoaPods
-
将此仓库添加到您的
Podfile
target 'Example' do # IMPORTANT: Make sure use_frameworks! is included at the top of the file use_frameworks! pod 'ClingConstraints' end
-
在终端中从 podfile 目录运行
pod install
-
打开 CocoaPods 创建的
.xcworkspace
-
完成!
示例
复制另一个视图的属性
// Creates and activates a constraint that makes thisView's height equal to thatView's
thisView.copy(.height, of: thatView)
具有个人空间的复制约束
// thisView copies thatView's height * 0.5 - 30.
thisView.copy(.height, of: thatView).withOffset(-30).withMultiplier(0.5)
单行内复制多个约束
// thisView copies the right, top, and left anchor constraints of that view-- in one line.
thisView.copy(.right, .top, .left, of: thatView)
约束合在一起
// thisView positions itself to the right of thatView with a spacing of 5
thisView.cling(.left, to: thatView, .right).withOffset(5)
填充视图
// Fills a view from the top to the bottom with the given views
thisView.fill(.topToBottom, withViews: [thatView1, thatView2], spacing: 0)
盒子里装的是什么?
约束创建
在任何UIView上,你可以调用以下函数。
注意,这些函数都会返回创建的约束。如果创建了多个约束,则返回一个约束列表。
// This view copies the other view's attributes (returns list of created constraints)
copy(_: NSLayoutAttribute..., of: UIView)
// This view copies the other view's attribute
copy(_: NSLayoutAttribute, of: UIView)
// Clings the calling view's attribute to the other view's attribute.
cling(_: NSLayoutAttribute, to: UIView, _: NSLayoutAttribute)
// Fills the calling view with the given FillMethod from left to right.
// FillMethods: .leftToRight, .rightToLeft, .topToBottom, .bottomToTop
fill(_: FillMethod, withViews: [UIView], withSpacing: CGFloat, spacesInternally: Bool = true)
// Sets the height for this view
setHeight(_: CGFloat)
// Sets the width for this view
setWidth(_: CGFloat)
约束属性链式调用
在任何NSLayoutConstraint上
withMultiplier(_: CGFloat)
withOffset(_: CGFloat)
withPriority(_: UILayoutPriority)
withRelation(_: NSLayoutRelation)
关于约束的激活和禁用
在任何约束集合中
activateAllConstraints()
deactivateAllConstraints()
示例项目
本存储库中的示例项目将展示如何使用约束创建上述动画。克隆此存储库,打开“示例项目”目录中的.xcodeproj
文件。
文档
阅读文档
作者
感谢Luis Padron帮助设置此项目!