KoalaTeaAutoLayout
在 John Sundell 的博客文章 Building DSLs in Swift 上建立的一个基于 John Sundell 在该博客文章中卓越工作的自定义 AutoLayout DSL 包装器。
示例
要运行示例项目,克隆仓库,然后从 Example 目录首先运行 pod install
要求
iOS 11.0+
安装
KoalaTeaAutoLayout 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'KoalaTeaAutoLayout'
用法
始终要首先导入
import KoalaTeaAutoLayout
然后您可以像这样使用库
let layoutView = UIView()
self.view.addSubview(layoutView)
layoutView.backgroundColor = .red
layoutView.layout {
$0.top == self.view.safeAreaLayoutGuide.topAnchor
$0.leading == self.view.leadingAnchor + 20
$0.trailing == self.view.trailingAnchor - 20
$0.height.equal(to: self.view.heightAnchor, multiplier: 0.1)
}
或者您可以将约束返回并随意激活/禁用它们
var constraints1: [NSLayoutConstraint] = []
var constraints2: [NSLayoutConstraint] = []
self.view.addSubview(layoutView)
layoutView.backgroundColor = .blue
constraints1 = layoutView.returnedLayout {
return [
$0.centerXAnchor == self.view.centerXAnchor,
$0.bottom == self.view.safeAreaLayoutGuide.bottomAnchor,
$0.height == 80,
$0.width == $0.height + 100,
]
}
constraints2 = layoutView.returnedLayout {
return [
$0.top == layoutView2.bottomAnchor,
$0.centerXAnchor == self.view.centerXAnchor,
$0.height == 80,
$0.width == $0.height + 100,
]
}
constraints2.deactivateAll()
animate()
func animate() {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.constraints1.deactivateAll()
self.constraints2.activateAll()
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
}, completion: { _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.constraints2.deactivateAll()
self.constraints1.activateAll()
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
}, completion: { _ in
self.animate()
})
}
})
}
}
还为常用的用例准备了方便的函数。
let edgesToSuperview = UIView()
edgesToSuperview.constrainEdgesToSuperview()
let centerToSuperview = UIView()
centerToSuperview.constrainCenterToSuperview()
let edgesToAnotherView = UIView()
edgesToAnotherView.constrainEdges(to: centerToSuperview)
贡献
打开问题报告。
打开一个PR。
或者给我发邮件,我会回复你。
作者
Craig Holliday
电子邮件:[email protected]
推特:https://twitter.com/TheMrHolliday
许可证
KoalaTeaAutoLayout遵循MIT许可证。有关更多信息,请参阅LICENSE文件。