为iOS和OS X的自动布局最终的API -- 使人印象深入的简单,很是强大。 PureLayout延伸的UIView /NSView , NSArray,和NSLayoutConstraint与以后苹果本身的框架,构建了一个全面的自动布局API 。 PureLayout是一个跨平台的Objective-C库,能够在伟大的Swift里工做(并查看!)。它彻底与iOS的支持自动布局全部版本和OS X的向后兼容。html
从头开始编写自动布局代码是不容易的。 PureLayout提供了自动布局彻底有能力和开发者友好的界面。它设计清晰和简洁,而且灵感来自Interface Builder能提供更大的灵活性的自动版式UI选项。该API还高效的,由于它仅增长一薄层的第三方代码和被设计以得到最佳性能。ios
PureLayout的当前版本支持全部版本的iOS和OS X的,由于每一个平台上推出自动布局,在这两个Swift和Objective-C ,用一个单一的代码库!git
pod 'PureLayout'
pod install
,而后打开你的应用程序的 .xcworkspace
文件启动的Xcode 。 PureLayout.h
头文件。 use_frameworks !
#import < PureLayout / PureLayout.h >
(或模块启用: @import PureLayout ;
)use_frameworks !
#import “ PureLayout.h ”
这就是它 - 如今去写一些漂亮的自动布局代码! PureLayout / PureLayout
项到您的 Cartfile.github "PureLayout/PureLayout"
carthage update
,而后按照额外步骤添加框架到你的项目。#import < PureLayout / PureLayout.h >
(或模块启用: @import PureLayout ;
)这就是它 - 如今去写一些漂亮的自动布局代码!github
PureLayout.h
头文件.#import "PureLayout.h"
到你的桥接头文件.#import "PureLayout.h"
这就是它 - 如今去写一些漂亮的自动布局代码!swift
要在应用程序扩展使用PureLayout ,你须要作一些额外的配置,以防止不可用的API的使用。 点击这里获取更多信息。api
发布的标签在使用Git的提交历史语义版本 。查看发布和发布说明为每一个版本。数组
这是核心API方法只是一个方便的概述。探索为全面的API [头文件](PureLayout / PureLayout) ,并找到相应.m文件的每一个方法的实现上面的完整文档。有两点要注意:安全
relation:
参数进行不平等约束。PureLayout定义了用于建立自动布局约束视图属性。这里是一个最经常使用的属性插图 。ruby
有5个特定的属性类型,其用于在大部分的API :app
ALEdge
ALDimension
ALAxis
ALMargin
在iOS8.0和更高版本可用ALMarginAxis
在iOS8.0和更高版本可用 *此外,还有一个通用属性类型, ALAttribute
,这是有效地全部特定类型的联合。你能够认为这是“父类”的全部具体属性类型的,这意味着它始终是安全蒙上了特定类型的通用 ALAttribute
类型。 (请注意,反之则否则 - 铸造的通常ALAttribute到一个特定的属性类型是不安全的! )
UIView
/NSView
- autoSetContent(CompressionResistance|Hugging)PriorityForAxis: - autoCenterInSuperview(Margins) // Margins 变体仅 iOS 8.0+ 使用 - autoAlignAxisToSuperview(Margin)Axis: // Margins 变体仅 iOS 8.0+ 使用 - autoPinEdgeToSuperview(Edge:|Margin:)(withInset:) // Margins 变体仅 iOS 8.0+ 使用 - autoPinEdgesToSuperview(Edges|Margins)(WithInsets:)(excludingEdge:) // Margins 变体仅 iOS 8.0+ 使用 - autoPinEdge:toEdge:ofView:(withOffset:) - autoAlignAxis:toSameAxisOfView:(withOffset:|withMultiplier:) - autoMatchDimension:toDimension:ofView:(withOffset:|withMultiplier:) - autoSetDimension(s)ToSize: - autoConstrainAttribute:toAttribute:ofView:(withOffset:|withMultiplier:) - autoPinTo(Top|Bottom)LayoutGuideOfViewController:withInset: // iOS only
NSArray
// 约束数组 - autoInstallConstraints - autoRemoveConstraints - autoIdentifyConstraints: // 仅iOS 7.0+, OS X 10.9+ // 视图数组 - autoAlignViewsToEdge: - autoAlignViewsToAxis: - autoMatchViewsDimension: - autoSetViewsDimension:toSize: - autoSetViewsDimensionsToSize: - autoDistributeViewsAlongAxis:alignedTo:withFixedSpacing:(insetSpacing:)(matchedSizes:) - autoDistributeViewsAlongAxis:alignedTo:withFixedSize:(insetSpacing:)
NSLayoutConstraint
+ autoCreateAndInstallConstraints: + autoCreateConstraintsWithoutInstalling: + autoSetPriority:forConstraints: + autoSetIdentifier:forConstraints: // iOS 7.0+, OS X 10.9+ only - autoIdentify: // iOS 7.0+, OS X 10.9+ only - autoInstall - autoRemove
PureLayout大大简化了编写自动布局代码。让咱们快速浏览一下一些例子,Swift使用PureLayout。
下面是使用PureLayout建立(自动启动)两种观点之间的约束:
view1.autoPinEdge(.Top, toEdge: .Bottom, ofView: view2)
若是没有PureLayout ,这里的等效代码,你就必须直接使用苹果的基础API写的:
NSLayoutConstraint(item: view1, attribute: .Top, relatedBy: .Equal, toItem: view2, attribute: .Bottom, multiplier: 1.0, constant: 0.0).active = true
PureLayout 不少 Api 建立多个约束为你引擎盖,让你写出可读性很强的布局代码:
// 2 constraints created & activated in one line! logoImageView.autoCenterInSuperview() // 4 constraints created & activated in one line! textContentView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsets(top: 20.0, left: 5.0, bottom: 10.0, right: 5.0))
PureLayout 老是返回它建立,以便您能够彻底控制的约束:
let constraint = skinnyView.autoMatchDimension(.Height, toDimension: .Width, ofView: tallView)
PureLayout 支持自动布局的全部功能,包括不平等、 优先事项、 版式边距、 标识符和更多。它是全面,开发者友好的方式来使用自动布局。
注: 文章由咱们iOS122的小伙伴 @ 静静
整理,喜欢就一块儿参与: iOS122 任务池