ALKit
简单易用的 AutoLayout 包装器,围绕 NSLayoutConstraints
使用。
需求
- Swift 4.2+
- iOS 8.0+
- tvOS 9.0+
安装
CocoaPods
use_frameworks!
pod 'ALKit'
手动
将 ALKit
文件夹复制到您的项目中
使用方法
初始化
可以从 Storyboard 或程序方式创建 UIView
实例。如果使用程序方式创建,请确保设置 view.translatesAutoresizingMaskIntoConstraints = false
。
包装器
所有套件的主要功能。包装了自动布局的 addConstraint:
方法。
func pin(
inView inView: UIView? = nil,
edge: NSLayoutAttribute,
toEdge: NSLayoutAttribute,
ofView: UIView?,
withInset: CGFloat = 0) {
let view = inView ?? ofView ?? self
view.addConstraint(NSLayoutConstraint(
item: self,
attribute: edge,
relatedBy: .Equal,
toItem: ofView,
attribute: toEdge,
multiplier: 1,
constant: withInset))
}
示例
box.fill(toView: view)
blue.pinTop(toView: box, withInset: 10)
blue.fillHorizontal(toView: box, withInset: 10)
blue.pinHeight(90)
red.pinBottom(toView: box, withInset: 10)
red.fillHorizontal(toView: box, withInset: 10)
red.pinHeight(90)
yellow.pinToTop(ofView: red, withOffset: 10)
yellow.pinCenterX(toView: red)
yellow.pinSize(width: 50, height: 50)
看起来是这样的