zoukankan      html  css  js  c++  java
  • PureLayout,使用纯代码写AutoLayout

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

  • 相关阅读:
    在pytorch中使用可视化工具visdom
    3"GAN-GAN实战:GD的实现
    31:GAN-js散度的缺陷以及解决办法:WGAN的提出
    31:GAN-DCGAN(deconvolution GAN逆卷积操作)以及JS散度的缺陷
    31:GAN-纳什均衡
    31:GAN-GAN原理
    30:无监督学习-自编码网络的变种
    30:无监督学习-自编码(Auto-Encoder简称AE)原理
    如何打印网络的参数量
    29:迁移学习(当数据集比较小时,可以使用迁移学习达到一个更好的效果)
  • 原文地址:https://www.cnblogs.com/ios122/p/4925619.html
Copyright © 2011-2022 走看看