测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2017年1月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Wagner Truppel 维护。
WTAutoLayoutExtensions 为 UIView
和 UILayoutGuide
增加了扩展,使得使用布局指南、布局锚点以及自动布局的其余部分更加简单和自然,具有一致的 API 和极少的代码。
以下是添加到 UIView
的完整 API 扩展集。有关详细信息,请参阅源文件中的完整文档。
public extension UIView {
/// Apple's standard distance between most UI elements, as a static property. No magic numbers!
static public let standardValue: CGFloat
/// Sets up commonly used priorities for the view's content compression resistance.
public func setupCommonContentCompressionResistancePriorities()
/// Sets up commonly used priorities for the view's content hugging property.
public func setupCommonContentHuggingPriorities()
/// Pins a view's edges to its superview's edges.
public func wtPinToSuperview(with edgeInsets: UIEdgeInsets,
useMargin: Bool,
useSuperviewMargin: Bool,
priority: UILayoutPriority,
active: Bool)
/// Centers a view in its superview.
public func wtCenterInSuperview(useMargin: Bool,
useSuperviewMargin: Bool,
priority: UILayoutPriority,
active: Bool)
/// Creates layout constraints for the **width** or the **height** of a view
/// or layout guide.
public static func wtConstraint(on attribute: NSLayoutAttribute,
_ item: WTAutoLayoutExtensionsAware,
_ relation: NSLayoutRelation,
_ constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// Creates layout constraints for the **aspect ratio** (width divided by height)
/// of a view or layout guide.
public static func wtAspectRatioConstraint(on item: WTAutoLayoutExtensionsAware,
_ relation: NSLayoutRelation,
_ ratio: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// Creates layout constraints between the **widths** or **heights** of two views,
/// two layout guides, or a view and a layout guide (in either order).
public static func wtConstraint(on attribute: NSLayoutAttribute,
_ item1: WTAutoLayoutExtensionsAware,
_ relation: NSLayoutRelation,
_ item2: WTAutoLayoutExtensionsAware,
times multiplier: CGFloat,
plus constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// Creates layout constraints to align two views, two layout guides, or a view
/// and a layout guide (in either order) on **compatible positional attributes**.
public static func wtConstraint(_ item1: WTAutoLayoutExtensionsAware,
_ attribute1: NSLayoutAttribute,
_ relation: NSLayoutRelation,
_ item2: WTAutoLayoutExtensionsAware,
_ attribute2: NSLayoutAttribute,
plus constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// Creates layout constraints to arrange two views, two layout guides, or a view
/// and a layout guide (in either order) along the **horizontal** direction.
public static func wtConstraint(_ item1: WTAutoLayoutExtensionsAware,
leading item2: WTAutoLayoutExtensionsAware,
offset relation: NSLayoutRelation,
_ constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// Creates layout constraints to arrange two views, two layout guides, or a view
/// and a layout guide (in either order) along the **horizontal** direction.
public static func wtConstraint(_ item1: WTAutoLayoutExtensionsAware,
trailing item2: WTAutoLayoutExtensionsAware,
offset relation: NSLayoutRelation,
_ constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// Creates layout constraints to arrange two views, two layout guides, or a view
/// and a layout guide (in either order) along the **vertical** direction.
public static func wtConstraint(_ item1: WTAutoLayoutExtensionsAware,
above item2: WTAutoLayoutExtensionsAware,
offset relation: NSLayoutRelation,
_ constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// Creates layout constraints to arrange two views, two layout guides, or a view
/// and a layout guide (in either order) along the **vertical** direction.
public static func wtConstraint(_ item1: WTAutoLayoutExtensionsAware,
below item2: WTAutoLayoutExtensionsAware,
offset relation: NSLayoutRelation,
_ constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// For the rare situation where the API defined above isn't sufficient, this
/// method lets you directly create a constraint on a dimensional **anchor**.
public static func wtConstraint(on dimensionalAnchor: NSLayoutDimension,
_ relation: NSLayoutRelation,
constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// For the rare situation where the API defined above isn't sufficient, this
/// method lets you directly create a constraint on a pair of dimensional **anchors**.
public static func wtConstraint(_ dimensionalAnchor1: NSLayoutDimension,
_ relation: NSLayoutRelation,
_ dimensionalAnchor2: NSLayoutDimension,
times multiplier: CGFloat,
plus constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// For the rare situation where the API defined above isn't sufficient, this
/// method lets you directly create a constraint on a pair of positional **anchors**.
public static func wtConstraint<T>(_ positionalAnchor1: NSLayoutAnchor<T>,
_ relation: NSLayoutRelation,
_ positionalAnchor2: NSLayoutAnchor<T>,
plus constant: CGFloat,
priority: UILayoutPriority,
active: Bool) -> NSLayoutConstraint
/// This method causes the layout guides of the recipient to be displayed as
/// rectangles of "marching ants" with the given color. Useful for debugging.
/// See acknowledgement note below.
public func showLayoutGuides(color: UIColor)
}
上面列出的完整 API 可能看起来既不简单也不自然,但以下是一些使用示例。这些示例直接取自附带的示例项目。
// unbroken leading-to-trailing chain
UIView.wtConstraint(view, .leadingMargin, .equal, leftGuide, .leading)
UIView.wtConstraint(leftGuide, .trailing, .equal, u1, .leading)
UIView.wtConstraint(u1, .trailing, .equal, middleGuide, .leading)
UIView.wtConstraint(middleGuide, .trailing, .equal, u2, .leading)
UIView.wtConstraint(u2, .trailing, .equal, rightGuide, .leading)
UIView.wtConstraint(rightGuide, .trailing, .equal, view, .trailingMargin)
// equal horizontal spaces
UIView.wtConstraint(on: .width, middleGuide, .equal, leftGuide)
UIView.wtConstraint(on: .width, middleGuide, .equal, rightGuide)
// set aspect ratios
UIView.wtAspectRatioConstraint(on: u1, .equal, 1)
UIView.wtAspectRatioConstraint(on: u2, .equal, 2)
// set heights
UIView.wtConstraint(on: .height, u1, .equal, 100)
UIView.wtConstraint(on: .height, u1, .equal, u2, times: 1.5, plus: 20)
// arrange vertically
UIView.wtConstraint(u1, .top, .equal, view, .topMargin, plus: 50)
UIView.wtConstraint(u1, above: u2, offset: .equal, 100)
// set layout guide heights
UIView.wtConstraint(leftGuide, .top, .equal, u1, .top)
UIView.wtConstraint(leftGuide, .bottom, .equal, u2, .bottom)
//
UIView.wtConstraint(middleGuide, .top, .equal, u1, .top)
UIView.wtConstraint(middleGuide, .bottom, .equal, u2, .bottom)
//
UIView.wtConstraint(rightGuide, .top, .equal, u1, .top)
UIView.wtConstraint(rightGuide, .bottom, .equal, u2, .bottom)
// set size
UIView.wtConstraint(on: .width, u3, .equal, 80)
UIView.wtConstraint(on: .height, u3, .equal, 80)
// center it
u3.wtCenterInSuperview()
如您所见,我希望您会同意,调用站的调用方法读起来非常自然。API 非常一致
UIView
的 静态
方法。_ =
。wtConstraint(on ...)
(唯一的例外是长宽比),而所讨论的项目或属性则作为第一个参数出现。on
命名。wt
前缀命名方法是有意为之,以防止将来向 UIView
添加类似命名的添加方法,并且 Xcode 的自动 Completion 会在您键入 wtc
后将列表缩小得相当好。UIView.wtConstraint(on: .height, u1, .equal, u2, times: 1.5, plus: 20)
.equal
。您也可以轻松地写入类似 UIView.wtConstraint(on: .width, u1, .greaterThanOrEqual, u2, times: 1.5, plus: 20)
或 UIView.wtConstraint(on: .width, u1, .lessThanOrEqual, u2)
的内容。要运行示例项目,请克隆代码库,然后首先从Examples目录中运行pod install
。
WTAutoLayoutExtensions的变更列在这里:https://github.com/wltrup/iOS-Swift-WTAutoLayoutExtensions/blob/master/CHANGELOG.md。
WTAutoLayoutExtensions仅支持iOS 9.0及更高版本。
WTAutoLayoutExtensions可通过CocoaPods获取。要安装它,只需在Podfile中添加以下行:
pod "WTAutoLayoutExtensions"
Wagner Truppel,[email protected]
WTAutoLayoutExtensions在MIT许可下可用。有关更多信息,请参阅LICENSE文件。
函数showLayoutGuides(color:)
及其支持代码是基于Jack Cox的工作和改编制作而成。查看他的博客文章和GitHub代码库。