UIKit LayoutExtension
LayoutExtension是iOS上UIKit自动布局的领域特定语言(DSL)。它基于行为驱动设计,使得最常用的语法最简单。
示例
要运行示例项目,首先克隆仓库,然后从示例目录中运行pod install
。
需求
所需部署目标为iOS >= 9.0
安装
CocoaPods
要通过CocoaPods安装LayoutExtension,只需将以下行添加到您的Podfile
中。
pod 'LayoutExtension'
Carthage
通过Carthage安装LayoutExtension,只需在您的Cartfile
中指定它即可。
github "abswift/LayoutExtension"
Swift Package Manager
通过Swift Package Manager安装LayoutExtension,将其添加到您的Package.swift
中的dependencies
值。
dependencies: [
.package(name: "LayoutExtension", url: "[email protected]:abswift/layout-extension.git", .upToNextMajor(from: "1.1.1"))
]
或者在Xcode中的文件 > Swift Packages > 添加包依赖…
菜单项中。
手动安装
如果您不想使用上述任何依赖管理器,可以通过将文件下载或克隆到您的项目中,手动将LayoutExtension安装到项目中。
使用方式
基本示例
self.view.addSubview(box) { subview in
subview.width(50)
subview.height(50)
subview.center()
}
当通过依赖管理器使用库时,请确保按以下方式导入库:
import LayoutExtension
语法
添加子视图的扩展
只有在子视图添加到其父视图时才能创建约束。因此,LayoutExtension根据驱动设计行为预测约束总是创建在子视图添加到父视图之后。
因此,对如下现有函数进行了以下扩展:
view.addSubview(button1) { subview in
subview.topSafe(40.0)
subview.centerX()
subview.height(50.0)
}
view.insertSubview(button2, at: 0) { subview in
subview.top(after: button1, offset: 30.0)
subview.centerX()
subview.height(50.0)
}
view.insertSubview(button3, belowSubview: button1) { subview in
subview.top(after: button1, offset: 30.0)
subview.left(40.0)
subview.right(40.0)
subview.height(50.0)
}
view.insertSubview(button4, aboveSubview: button1) { subview in
subview.top(after: button1, offset: 30.0)
subview.centerX()
subview.height(50.0)
}
视图扩展的完整列表
所有方法都返回 NSLayoutConstraint
对象。语法如下
- 所有方法都返回激活的
NSLayoutConstraint
或约束的元组 - 所有方法都是
@discardableResult
constant
的默认值始终是0.0
multiplier
的默认值始终是1.0
priority
的默认值始终是.required
,并且这是最后一个参数
大小到常量
UIView.width(_ width: priority:)
UIView.widthLess(_ width: priority:)
UIView.widthGreater(_ width: priority:)
UIView.height(_ height: priority:)
UIView.heightLess(_ height: priority:)
UIView.heightGreater(_ height: priority:)
大小到维度
UIView.width(to <anchor>: multiplier: constant: priority:)
UIView.widthLess(than <anchor>: multiplier: constant: priority:)
UIView.widthGreater(than <anchor>: multiplier: constant: priority:)
UIView.height(to <anchor>: multiplier: constant: priority:)
UIView.heightLess(than <anchor>: multiplier: constant: priority:)
UIView.heightGreater(than <anchor>: multiplier: constant: priority:)
边到父元素
UIView.leading(_ inset: priority:)
UIView.leadingLess(_ inset: priority:)
UIView.leadingGreater(_ inset: priority:)
UIView.trailing(_ inset: priority:)
UIView.trailingLess(_ inset: priority:)
UIView.trailingGreater(_ inset: priority:)
UIView.left(_ inset: priority:)
UIView.leftLess(_ inset: priority:)
UIView.leftGreater(_ inset: priority:)
UIView.right(_ inset: priority:)
UIView.rightLess(_ inset: priority:)
UIView.rightGreater(_ inset: priority:)
UIView.top(_ inset: priority:)
UIView.topLess(_ inset: priority:)
UIView.topGreater(_ inset: priority:)
UIView.bottom(_ inset: priority:)
UIView.bottomLess(_ inset: priority:)
UIView.bottomGreater(_ inset: priority:)
填充到父元素
UIView.fill(_ inset: priority:)
UIView.fill(_ insets: priority:)
UIView.fillLeadingTrailing(_ inset: priority:)
UIView.fillLeadingTrailing(_ insets: priority:)
边到父元素的安全区域
UIView.leadingSafe(_ inset: priority:)
UIView.trailingSafe(_ inset: priority:)
UIView.leftSafe(_ inset: priority:)
UIView.rightSafe(_ inset: priority:)
UIView.topSafe(_ inset: priority:)
UIView.bottomSafe(_ inset: priority:)
填充到父元素的 безопасность区域
UIView.fillSafe(_ inset: priority:)
UIView.fillSafe(_ insets: priority:)
UIView.fillLeadingTrailingSafe(_ inset: priority:)
UIView.fillLeadingTrailingSafe(_ insets: priority:)
边缘到另一个视图的边缘
这在很多情况下都是非常有用的,并且相当棘手。
UIView.leading(after <view>: offset: priority:)
UIView.leadingLess(after <view>: offset: priority:)
UIView.leadingGreater(after <view>: offset: priority:)
UIView.trailing(before <view>: offset: priority:)
UIView.trailingLess(before <view>: offset: priority:)
UIView.trailingGreater(before <view>: offset: priority:)
UIView.left(after <view>: offset: priority:)
UIView.leftLess(after <view>: offset: priority:)
UIView.leftGreater(after <view>: offset: priority:)
UIView.right(before <view>: offset: priority:)
UIView.rightLess(before <view>: offset: priority:)
UIView.rightGreater(before <view>: offset: priority:)
UIView.top(after <view>: offset: priority:)
UIView.topLess(after <view>: offset: priority:)
UIView.topGreater(after <view>: offset: priority:)
UIView.bottom(before <view>: offset: priority:)
UIView.bottomLess(before <view>: offset: priority:)
UIView.bottomGreater(before <view>: offset: priority:)
边缘到锚点
UIView.leading(to <anchor>: offset: priority:)
UIView.leadingLess(than <anchor>: offset: priority:)
UIView.leadingGreater(than <anchor>: offset: priority:)
UIView.trailing(to <anchor>: offset: priority:)
UIView.trailingLess(than <anchor>: offset: priority:)
UIView.trailingGreater(than <anchor>: offset: priority:)
UIView.left(to <anchor>: offset: priority:)
UIView.leftLess(than <anchor>: offset: priority:)
UIView.leftGreater(than <anchor>: offset: priority:)
UIView.right(to <anchor>: offset: priority:)
UIView.rightLess(than <anchor>: offset: priority:)
UIView.rightGreater(than <anchor>: offset: priority:)
UIView.top(to <anchor>: offset: priority:)
UIView.topLess(than <anchor>: offset: priority:)
UIView.topGreater(than <anchor>: offset: priority:)
UIView.bottom(to <anchor>: offset: priority:)
UIView.bottomLess(than <anchor>: offset: priority:)
UIView.bottomGreater(than <anchor>: offset: priority:)
边缘到视图控制器安全区域(布局指南)
本节中所有内容均已在iOS 11.0中弃用:@available(iOS, deprecated: 11.0)
.
UIView.leadingSafe(to <view controller>: offset: priority:)
UIView.trailingSafe(to <view controller>: offset: priority:)
UIView.leftSafe(to <view controller>: offset: priority:)
UIView.rightSafe(to <view controller>: offset: priority:)
UIView.topSafe(to <view controller>: offset: priority:)
UIView.bottomSafe(to <view controller>: offset: priority:)
居中对齐到父元素
UIView.centerX(_ offset: priority:)
UIView.centerXLess(_ offset: priority:)
UIView.centerXGreater(_ offset: priority:)
UIView.centerY(_ offset: priority:)
UIView.centerYLess(_ offset: priority:)
UIView.centerYGreater(_ offset: priority:)
UIView.center(_ offset <point>: priority:)
居中对齐到另一个视图的中心
UIView.centerX(to <view>: offset: priority:)
UIView.centerXLess(than <view>: offset: priority:)
UIView.centerXGreater(than <view>: offset: priority:)
UIView.centerY(to <view>: offset: priority:)
UIView.centerYLess(than <view>: offset: priority:)
UIView.centerYGreater(than <view>: offset: priority:)
居中对齐到锚点
UIView.centerX(to <anchor>: offset: priority:)
UIView.centerXLess(than <anchor>: offset: priority:)
UIView.centerXGreater(than <anchor>: offset: priority:)
UIView.centerY(to <anchor>: offset: priority:)
UIView.centerYLess(than <anchor>: offset: priority:)
UIView.centerYGreater(than <anchor>: offset: priority:)
作者
Ariel Bogdziewicz @ AB软件
许可
LayoutExtension在MIT许可下可用。有关更多信息,请参阅LICENSE文件。