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的向後兼容。html

從頭開始編寫自動佈局代碼是不容易的。 PureLayout提供了自動佈局徹底有能力和開發者友好的界面。它設計清晰和簡潔,而且靈感來自Interface Builder能提供更大的靈活性的自動版式UI選項。該API還高效的,由於它僅增長一薄層的第三方代碼和被設計以得到最佳性能。ios

目錄

  1. 建立
  2. API備註
  3. 用法
  1. PureLayout vs. Apple框架
  2. 問題,建議,引入請求?

建立

兼容性

PureLayout的當前版本支持全部版本的iOS和OS X的,由於每一個平臺上推出自動佈局,在這兩個Swift和Objective-C ,用一個單一的代碼庫!git

  • 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

手動從GitHub下載

  1. 下載的源文件 PureLayout subdirectory.
  2. 源文件添加到您的Xcode項目。
  3. 添加 PureLayout.h 頭文件.
  • Swift: 添加 #import "PureLayout.h" 到你的橋接頭文件.
  • Objective-C: #import "PureLayout.h"

這就是它 - 如今去寫一些漂亮的自動佈局代碼!swift

App 擴展

要在應用程序擴展使用PureLayout ,你須要作一些額外的配置,以防止不可用的API的使用。 點擊這裏獲取更多信息。api

發佈

發佈的標籤在使用Git的提交歷史語義版本 。查看發佈和發佈說明爲每一個版本。數組

API 備註

這是核心API方法只是一個方便的概述。探索爲全面的API 頭文件 ,並找到相應.m文件的每一個方法的實現上面的完整文檔。有兩點要注意:安全

  • 全部的公共API方法命名空間的前綴`auto...... ' ,這也很容易讓Xcode的爲您鍵入自動完成。
  • 創造的約束也會自動安裝(激活)約束的方法,而後返回新的約束 ,您能夠選擇存儲供之後調整或刪除。
  • 許多方法等級也有一個變體,它包括一個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

用法

實例代碼 (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 任務池

相關文章
相關標籤/搜索