A carefully-crafted category on UIView
(and a one-method category on NSLayoutConstraint
) that provides a simpler interface for creating Auto Layout constraints.
The goal is to provide a pleasant API for the vast majority of common Auto Layout use cases. It's designed for clarity and simplicity while simultaneously minimizing the amount of third party code. The API takes inspiration from the Auto Layout UI options available in Interface Builder.
This is just a handy overview of the primary methods. Check out the header file for the full API and documentation.
Note: all of the API methods begin with auto...
for easy autocompletion!
UIView
- + removeConstraint(s):
- - removeConstraintsAffectingView
- - removeConstraintsAffectingViewAndSubviews
- + autoSetPriority:forConstraints:
- - autoCenterInSuperview(AlongAxis:)
- - autoPinCenterAxis:toPositionInSuperview:
- - autoPinEdge:toPositionInSuperview:
- - autoPinEdge(s)ToSuperviewEdge(s):withInset(s):
- - autoPinEdge:toEdge:ofView:(withOffset:)
- - autoAlignAxis:toSameAxisOfView:(withOffset:)
- - autoMatchDimension:toDimension:ofView:(withOffset:)
- - autoMatchDimension:toDimension:ofView:(withMultiplier:)
- - autoSetDimension(s)ToSize:
- - autoPinToTopLayoutGuideOfViewController:withInset:
- - autoPinToBottomLayoutGuideOfViewController:withInset:
Advanced methods that layout an array of subviews:
- - autoAlignSubviews:toEdge:
- - autoAlignSubviews:toAxis:
- - autoMatchSubviews:dimension:
- - autoSetSubviews:dimension:toSize:
- - autoDistributeSubviews:alongAxis:withFixedSpacing:alignment:
- - autoDistributeSubviews:alongAxis:withFixedSize:alignment:
NSLayoutConstraint
- - remove
Note: you must be developing for iOS 6.0 or later to use Auto Layout.
Using CocoaPods
-
Add the pod
UIView-AutoLayout
to your Podfile.platform :ios, '6.0' pod 'UIView-AutoLayout'
-
Run
pod install
from Terminal. -
Open your app's
.xcworkspace
file to launch Xcode and start joyfully creating constraints in code!
Manually from GitHub
-
Download the
UIView+AutoLayout.h
andUIView+AutoLayout.m
files and add them to your Xcode projectwget https://raw.github.com/smileyborg/UIView-AutoLayout/master/Source/UIView+AutoLayout.h wget https://raw.github.com/smileyborg/UIView-AutoLayout/master/Source/UIView+AutoLayout.m
-
#import UIView+AutoLayout.h
wherever you need it(Hint: adding the import to your precompiled header file once will allow you to access the API from anywhere in your app!)
-
Start joyfully creating constraints in code!
Releases
Releases are tagged in the git commit history using semantic versioning. Check out the releases and release notes for each version.
Example Project
Check out the example project included in the repository. It contains a few demos of the API in use for various scenarios. While running the app, tap on the screen to cycle through the demos. You can rotate the device to see the constraints in action (as well as toggle the taller in-call status bar in the iOS Simulator).
Tips and Tricks
Check out some Tips and Tricks to keep in mind when using the API.
- Will need to use the
NSLayoutConstraint
SDK API directly for some uncommon advanced use cases
An overview of the Auto Layout options available, ordered from the lowest- to highest-level of abstraction.
- Apple NSLayoutConstraint SDK API
- Pros: Raw power
- Cons: Extremely verbose, tedious to write, difficult to read
- Apple Visual Format Language
- Pros: Concise, convenient
- Cons: Doesn't support some use cases, incomplete compile-time checks, must learn syntax, hard to debug
- Apple Interface Builder
- Pros: Visual, simple
- Cons: Difficult for complicated layouts, cannot dynamically set constraints, not always WYSIWYG
- UIView+AutoLayout
- Pros: Simple, built directly on top of the SDK, minimal third party code
- Cons: Not the most concise or pure expression of layout code
- High-level layout frameworks (Masonry, KeepLayout)
- Pros: Very clean, simple, and convenient
- Cons: Cannot mix with SDK APIs, total dependency on third party code, potential compatibility issues when SDK changes
Bring 'em on! :)
I'm especially interested in hearing about any common use cases that this API does not currently address. Feel free to add feature requests (and view current work in progress) on the Feature Requests page of the wiki for this project.
Originally forked from Rich Turton's jrturton/UIView-Autolayout. Designed & maintained by Tyler Fox. Distributed with the MIT License.