PureLayout,使用纯代码写AutoLayout

PureLayout是一个跨平台的Objective-C库,适用于iOS和OSX的自动布局,提供简单、强大且开发者友好的API。它兼容Swift和Objective-C,支持所有iOS和OSX版本,通过减少代码量和提升性能,简化自动布局的编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PureLayout

Build Status Test Coverage Version Platform License

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

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

目录

  1. 创建
  2. API备注
  3. 用法
  1. PureLayout vs. Apple框架
  2. 问题,建议,引入请求?

创建

兼容性

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

  1. 添加' PureLayout`到你的 Podfile.
pod 'PureLayout'
  1. 运行终端pod install ,然后打开你的应用程序的.xcworkspace文件启动的Xcode 。
  2. 导入PureLayout.h头文件。
  • 在你Podfile文件使用use_frameworks !
    • Swift:'import PureLayout`
    • Objective-C的: #import < PureLayout / PureLayout.h > (或模块启用: @import PureLayout ;
  • 如果你Podfile里没有use_frameworks !
    • Swift:添加` #import “ PureLayout.h ” '你的桥接头文件。
    • Objective-C的: #import “ PureLayout.h ” 这就是它 - 现在去写一些漂亮的自动布局代码!

运用 Carthage

  1. 添加PureLayout / PureLayout项到您的 Cartfile.
github "PureLayout/PureLayout"
  1. 运行carthage update ,然后按照额外步骤添加框架到你的项目。
  2. 导入PureLayout框架/模块。
  • Swift:'import PureLayout`
  • Objective-C: #import < PureLayout / PureLayout.h > (或模块启用: @import PureLayout ;

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

手动从GitHub下载

  1. 下载的源文件 PureLayout subdirectory.
  2. 源文件添加到您的Xcode项目。
  3. 添加 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

- 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

用法

实例代码 (Swift)

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 任务池

转载于:https://my.oschina.net/ios122/blog/524318

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值