PinLayout
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]
Install / Use
/learn @layoutBox/PinLayoutREADME
Extremely Fast views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. PinLayout can layouts UIView, NSView and CALayer.
"No Auto layout constraints attached"
Requirements
- iOS 9.0+ / tvOS 9.0+ / macOS 10.9+
- Swift 5.x / 4 / 3 / Objective-C
- Xcode 13 / 12 / 11 / 10
Recent changes/features
- :star: Add
pin.keyboardAreaproperty [iOS 15+] - :star: New chainable Objective-C syntax. See PinLayout using Objective-C
- :star: Automatic Sizing, use PinLayout to compute view size. See Automatic sizing
- :star: Add methods to position a view between two other views. See Layout between other views.
- :star: Add
pin.readableMarginsandpin.layoutMarginsproperties. - :star: Add
sizeToFit()method. See Adjusting size. - :star: PinLayout can now layout CALayer. See CALayer Support for more information.
- :star: PinLayout is #8 in the list of Swift Layout frameworks on Awesome Swift
- See Changelog for all changes.
Content
- Introduction examples
- PinLayout principles and philosophy
- Performance
- Documentation
- Right to left languages (RTL) support
- Edges layout
- Relative Edges layout
- Layout between other views
- Edges
- Anchors
- Width, height and size
- Adjusting size
- minWidth, maxWidth, minHeight, maxHeight
- Margins
- Aspect Ratio
- safeArea, readable and layout margins
- WrapContent
- justify, align
- Automatic sizing
- UIView's transforms
- Warnings
- Animations using PinLayout
- More examples
- Examples App
- macOS Support
- CALayer Support
- PinLayout in Xcode Playgrounds
- PinLayout using Objective-C
- Installation
- FAQ
- Comments, ideas, suggestions, issues, ...
:pushpin: PinLayout is actively updated. So please come often to see latest changes. You can also Star it to be able to retrieve it easily later.
PinLayout and layoutBox
<a href="https://github.com/layoutBox/PinLayout"><img src="docs/images/pinlayout_plus_layoutBox.png" width="200"/></a>
PinLayout is part of the layoutBox organization containing few Open Source projects related to layout using Swift. See layoutBox.
PinLayout + Autolayout
You don't need to choose, you can layout some views using PinLayout and some other with autolayout. Your views just to need to implement the autolayout intrinsicContentSize properties.
<a name="intro_usage_examples"></a>
Introduction examples
Example 1:
This example layout an image, a UISegmentedControl, a label and a line separator. This example adjusts its content to match the device's size and orientation changes.
- UIImageView's size is 100x100 and layouted below the UINavigationBar with a margin of 10 pixels all around.
- UISegmentedControl is at the right of the logo image, use the remaining horizontal space with a left and right margin of 20 pixels.
- UILabel is below the UISegmentedControl with a top margin of 10 pixels. Its width matched the UISegmentedControl's width. The label is multiline, so its height must be adjusted to fit its width.
- Separator is below the UIImageView and the UILabel, i.e. below the tallest one. The separator has a top margin of 10 pixels, left-aligned to the UIImageView and right-aligned to the UISegmentedControl.
<a href="https://github.com/layoutBox/PinLayout/blob/master/Example/PinLayoutSample/UI/Examples/Intro/IntroView.swift"><img src="docs/images/pinlayout_intro_example_iphonex.png"/></a>
override func layoutSubviews() {
super.layoutSubviews()
let padding: CGFloat = 10
logo.pin.top(pin.safeArea).left(pin.safeArea).width(100).aspectRatio().margin(padding)
segmented.pin.after(of: logo, aligned: .top).right(pin.safeArea).marginHorizontal(padding)
textLabel.pin.below(of: segmented, aligned: .left).width(of: segmented).pinEdges().marginTop(10).sizeToFit(.width)
separatorView.pin.below(of: [logo, textLabel], aligned: .left).right(to: segmented.edge.right).marginTop(10)
}
- 4 views, 4 lines
- PinLayout expose the
safeAreaInsetsthroughUIView.pin.safeArea, this property support not only iOS 11, but is also backward compatible for earlier iOS releases (7/8/9/10). See safeAreaInsets support for more information. - PinLayout doesn't use auto layout constraints, it is a framework that manually layout views. For that reason you need to update the layout inside either
UIView.layoutSubviews()orUIViewController.viewDidLayoutSubviews()to handle container size's changes, including device rotation. You'll also need to handle UITraitCollection changes for app's that support multitasking. In the example above PinLayout's commands are inside UIView'slayoutSubviews()method. - This example is available in the Examples App. See example complete source code
Example 2:
This example shows how easily PinLayout can adjust its layout based on the view's container size.
- If the container's width is smaller than 500 pixels, the label takes the full width and the UISegmentedControl is placed below it.
- If the container's width is greater or equal to 500 pixels, the UISegmentedControl is at the top-right corner and the label takes the remaining horizontal space.
<a href="https://github.com/layoutBox/PinLayout/blob/master/Example/PinLayoutSample/UI/Examples/AdjustToContainer/Subviews/ChoiceSelectorView.swift"><img src="docs/pinlayout_example_adjust_to_container2.png" width="640"/></a>
let margin: CGFloat = 12
if frame.width < 500 {
textLabel.pin.top().horizontally().margin(margin).sizeToFit(.width)
segmentedControl.pin.below(of: textLabel).right().margin(margin)
} else {
segmentedControl.pin.top().right().margin(margin)
textLabel.pin.top().left().before(of: segmentedControl).margin(margin).sizeToFit(.width)
}
:pushpin: This example is available in the Examples App. See example complete source code
<a name="introduction"></a>
PinLayout principles and philosophy
- Manual layouting (doesn't rely on auto layout).
- PinLayout exist to be simple and fast as possible! In fact, it is fast as manual layouting. See performance results below.
- Full control: You're in the middle of the layout process, no magic black box.
- Layout one view at a time. Make it simple to code and debug.
- Concise syntax. Layout most views using a single line.
- See the complete list here....
<a name="performance"></a>
PinLayout's Performance
PinLayout's performance has been measured using the Layout Framework Benchmark.
As you can see in the following chart, PinLayout are faster or equal to manual layouting, and between 8x and 12x faster than auto layout, and this for all types of iPhone (5S/6/6S/7/8/X)
See here for more details, results and explanation of the benchmark.
<p align="center"> <a href="docs/Benchmark.md"> <img src="docs/Benchmark/benchmark_comparison_all_small.png" width=660/> </a> </p><a name="documentati
