您的位置:首页 > 编程语言

PureLayout,使用纯代码写AutoLayout

2015-10-31 15:30 295 查看















为iOS和OS X的自动布局最终的API – 令人印象深刻的简单,非常强大。 PureLayout延伸的UIView /NSView , NSArray,和NSLayoutConstraint与之后苹果自己的框架,构建了一个全面的自动布局API 。 PureLayout是一个跨平台的Objective-C库,可以在伟大的Swift里工作(并查看!)。它完全与iOS的支持自动布局所有版本和OS X的向后兼容。

从头开始编写自动布局代码是不容易的。 PureLayout提供了自动布局完全有能力和开发者友好的界面。它设计清晰和简洁,并且灵感来自Interface Builder能提供更大的灵活性的自动版式UI选项。该API还高效的,因为它仅增加一薄层的第三方代码和被设计以获得最佳性能。

目录

创建

API备注

用法

示例代码

示例应用程序

PureLayout vs. Apple框架

问题,建议,引入请求?

创建

兼容性

PureLayout的当前版本支持所有版本的iOS和OS X的,因为每个平台上推出自动布局,在这两个Swift和Objective-C ,用一个单一的代码库!

Xcode

语言支持: Swift (任何版本), Objective-C

完全兼容: Xcode 7.0

支持的最低版本: Xcode 5.0

iOS

完全兼容: iOS 9.0

最低部署版本: iOS 6.0

OS X

完全: OS X 10.11

最低部署版本: OS X 10.7

运用 CocoaPods

添加’ PureLayout`到你的 Podfile.

[code]pod 'PureLayout'


运行终端
pod install
,然后打开你的应用程序的
.xcworkspace
文件启动的Xcode 。

导入
PureLayout.h
头文件。

在你Podfile文件使用
use_frameworks !


Swift:’import PureLayout
</li> 

<li>Objective-C的:
#import < PureLayout / PureLayout.h >
(或模块启用:
@import PureLayout ;
)</li> 

<li>如果你Podfile里没有
use_frameworks !
</li> 

<li>Swift:添加
#import “ PureLayout.h ” ‘你的桥接头文件。

Objective-C的:
#import “ PureLayout.h ”


这就是它 - 现在去写一些漂亮的自动布局代码!

运用 Carthage

添加
PureLayout / PureLayout
项到您的 Cartfile.

[code]github "PureLayout/PureLayout"


运行
carthage update
,然后按照额外步骤添加框架到你的项目。

导入PureLayout框架/模块。

Swift:’import PureLayout
</li> 

<li>Objective-C:
#import < PureLayout / PureLayout.h >
(或模块启用:
@import PureLayout ; ` )

这就是它 - 现在去写一些漂亮的自动布局代码!

手动从GitHub下载

下载的源文件 PureLayout subdirectory.

源文件添加到您的Xcode项目。

添加
PureLayout.h
头文件.

Swift: 添加
#import "PureLayout.h"
到你的桥接头文件.

Objective-C:
#import "PureLayout.h"


这就是它 - 现在去写一些漂亮的自动布局代码!

App 扩展

要在应用程序扩展使用PureLayout ,你需要做一些额外的配置,以防止不可用的API的使用。 点击这里获取更多信息。

发布

发布的标签在使用Git的提交历史语义版本 。查看发布和发布说明为每个版本。

API 备注

这是核心API方法只是一个方便的概述。探索为全面的API [头文件](PureLayout / PureLayout) ,并找到相应.m文件的每个方法的实现上面的完整文档。有两点要注意:

所有的公共API方法命名空间的前缀`auto…… ’ ,这也很容易让Xcode的为您键入自动完成。

创造的约束也会自动安装(激活)约束的方法,然后返回新的约束 ,您可以选择存储供以后调整或删除。

许多方法等级也有一个变体,它包括一个
relation:
参数进行不平等约束。

属性

PureLayout定义了用于创建自动布局约束视图属性。这里是一个最常用的属性插图

有5个特定的属性类型,其用于在大部分的API :

ALEdge


ALDimension


ALAxis


ALMargin
在iOS8.0和更高版本可用

ALMarginAxis
在iOS8.0和更高版本可用 *

此外,还有一个通用属性类型,
ALAttribute
,这是有效地所有特定类型的联合。你可以认为这是“父类”的所有具体属性类型的,这意味着它始终是安全蒙上了特定类型的通用
ALAttribute
类型。 (请注意,反之则不然 - 铸造的一般ALAttribute到一个特定的属性类型是不安全的! )

UIView
/
NSView

[code]- 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

[code]// 约束数组
- 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

[code]+ 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


用法

实例代码 (Swift)

PureLayout大大简化了编写自动布局代码。让我们快速浏览一下一些例子,Swift使用PureLayout。

下面是使用PureLayout创建(自动启动)两种观点之间的约束:

[code]view1.autoPinEdge(.Top, toEdge: .Bottom, ofView: view2)


如果没有PureLayout ,这里的等效代码,你就必须直接使用苹果的基础API写的:

[code]NSLayoutConstraint(item: view1, attribute: .Top, relatedBy: .Equal, toItem: view2, attribute: .Bottom, multiplier: 1.0, constant: 0.0).active = true


PureLayout 很多 Api 创建多个约束为你引擎盖,让你写出可读性很强的布局代码:

[code]// 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 总是返回它创建,以便您可以完全控制的约束:

[code]let constraint = skinnyView.autoMatchDimension(.Height, toDimension: .Width, ofView: tallView)


PureLayout 支持自动布局的所有功能,包括不平等、 优先事项、 版式边距、 标识符和更多。它是全面,开发者友好的方式来使用自动布局。

注: 文章由我们iOS122的小伙伴
@ 静静
整理,喜欢就一起参与: iOS122 任务池
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: