KVConstraintKit
KVConstraintKit 是 Swift 的一种声明式语言,用于在 iOS、tvOS 和 OSX 上制作简单且令人印象深刻的 Auto Layout 约束
安装
使用 CocoaPods
- 要使用 CocoaPods 将
KVConstraintKit
集成到您的 Xcode 项目中,请在您的 Podfile 中指定目标
use_frameworks!
pod 'KVConstraintKit', '~> 1.2.0'
如果您想走在前沿,将最后一行替换为
pod 'KVConstraintKit', :git => 'https://github.com/keshavvishwkarma/KVConstraintKit.git', :branch => 'master'
- 运行
pod install
并打开{Project}.xcworkspace
而不是{Project}.xcodeproj
文件以启动 Xcode。
使用 Carthage
- 要使用 Carthage 将 KVConstraintKit 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它。
github "keshavvishwkarma/KVConstraintKit" ~> 1.2.0
- 运行
carthage update
并按照附加步骤将KVConstraintKit
添加到您的项目中。
自动布局属性
KVConstraintKit
支持 iOS、tvOS 和 OSX 中所有内置的布局属性,请参阅 NSLayoutAttribute 枚举。
用法
以下是一个快速示例:
不使用 KVConstraintKit
let v = UIView.prepareAutoLayoutView()
v.backgroundColor = UIColor.red
view.addSubview(v)
// Prepare constraints and then add them on superview of view
let top = NSLayoutConstraint( item: v, attribute: .top,
relatedBy: .equal,
toItem: v.superview!, attribute: .top,
multiplier: 1.0, constant: 0)
let leading = NSLayoutConstraint( item: v, attribute: .leading,
relatedBy: .equal,
toItem: v.superview!, attribute: .leading,
multiplier: 1.0, constant: 0)
let trailing = NSLayoutConstraint( item: v, attribute: .trailing,
relatedBy: .equal,
toItem: v.superview!, attribute: .trailing,
multiplier: 1.0, constant: 0)
let bottom = NSLayoutConstraint( item: v, attribute: .bottom,
relatedBy: .equal,
toItem: v.superview!, attribute: .bottom,
multiplier: 1.0, constant: 0)
v.superview?.addConstraints([top, leading, trailing, bottom])
使用 KVConstraintKit
v +== [.top, .leading, .trailing, .bottom]
对于边距约束类似
v +== [ .leadingMargin, .trailingMargin, .topMargin, .bottomMargin]
Fit(适配)
水平方向
view.fitHorizontallyToSuperview()
OR
view.fitHorizontallyToSuperview(20) // padding
垂直方向
view.fitVerticallyToSuperview()
OR
view.fitVerticallyToSuperview(20) // padding
水平和垂直方向
view.fitToSuperview()
OR
view.fitToSuperview(20) // width same padding for all edge
带内边距的 Fit
let inset = UIEdgeInsets(top: 4, left: 8, bottom: 12, right: 16)
view.fitToSuperview(contentInset:inset)
居中
水平方向
view.applyCenterX()
OR
view.applyCenterX(20) // X offset
垂直方向
view.applyCenterY()
OR
view.applyCenterY(20) // Y offset
水平和垂直方向
view.applyCenter()
OR
view.applyCenter(CGPoint(x:20, y:20)) // XY offset
OR
view.applyCenterX(4).applyCenterY(16) // XY offset
大小
宽度
view.applyWidth(100)
OR
view.applyAtLeastWidth(100)
OR
view.applyAtMostWidth(100)
高度
view.applyHeight(100)
OR
view.applyAtLeastHeight(100)
OR
view.applyAtMostHeight(100)
纵横比
view.applyAspectRatio()
快速参考
详情请参阅 ApplyViewConstraint 扩展和 LayoutRelationable 协议。
注意: 避免使用 Left 和 Right 属性,请改用 Leading 和 Trailing。这允许布局适应视图的阅读方向。默认情况下,阅读方向基于用户设置的当前语言。
自定义操作符
KVConstraintKit
提供了以下类型的操作符
,用于添加
、移除
、访问
和修改
约束。
操作符 | 含义 |
---|---|
+ | 添加约束 |
- | 移除约束 |
* | 修改约束的乘数 |
~ | 修改优先级 (UILayoutPriority)、关系 (NSLayoutRelation)和替换 约束 |
<- | 通过属性(例如:NSLayoutAttribute.Height)访问约束 |
+== | 添加相等关系(NSLayoutRelation.Equal)约束 |
+>= | 添加大于等于关系(NSLayoutRelation.GreaterThanOrEqual)约束 |
+<= | 添加小于等于关系(NSLayoutRelation.LessThanOrEqual)约束 |
*== | 添加具有乘数 的相等关系约束 |
*>= | 添加具有乘数 的大于等于关系约束 |
*<= | 添加具有乘数 的小于等于关系约束 |
|==| | 在兄弟视图中添加或等于关系约束 |
|>=| | 在兄弟视图中添加大于等于关系约束 |
|<=| | 在兄弟视图中添加小于等于关系约束 |
许可
KVConstraintKit
可在MIT许可下使用。有关更多信息,请参阅LICENSE文件。
贡献
欢迎任何形式的贡献!您可以通过GitHub上的pull请求和问题来进行贡献。
作者
如果您想与我联系,请发送电子邮件至:[email protected]