爲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 頭文件 ,並找到相應.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 任務池