FixFlex
FixFlex is a simple yet powerful Auto Layout library built on top of the NSLayoutAnchor API, a swifty and type-safe reimagination of Visual Format Language
Features
- Auto Layout code that is easy to write, read, and modify
- Simple API with 2 functions and 4 specifiers, covering 99% of layout use cases
- Lightweight, implementation is only 300 lines of code
- Compatible with any other Auto Layout code
- Basically generates a bunch of activated NSLayoutConstraint and UILayoutGuide
- Super straightforward mental model
- Typesafe alternative to VFL
- Dynamic Type and Right-To-Left friendly
- Automatically sets translatesAutoresizingMaskIntoConstraints to false
- Supports iOS 12.0+ / Mac OS X 10.13+ / tvOS 12.0+
Usage
Imagine we want to create a layout like this:
- Let's scan the layout horizontally and translate it into FixFlex code:
Most of the views and spacings have a fixed width (Fix
), while the title and subtitle widths are flexible, designed to occupy the remaining space (Flex
):
- Vertically, we have three distinct groups of views. Starting with the icon:
We do a spacing at the top using Fix
. The bottom spacing should be at least 15pt, for the case when the labels' height is less than the icon's height:
- Next, we perform a vertical scan of the title and subtitle:
- Finally, we scan the chevron vertically:
To center the chevron, we ensure the top spacing is equal to the bottom spacing using Grow
:
That's it! The best part is how easy it is to modify FixFlex layout code, inserting extra padding or views effortlessly, without the need to rewire constraints.
API
hput/vput
FixFlex
provides two functions for laying out views horizontally (hput
) and vertically (vput
), accessible through the view.fx.*
namespace.
You can specify startAnchor
/endAnchor
to layout items between arbitrary anchors instead of the view's edges. startOffset
/endOffset
are used to add spacing or offsets from the startAnchor
and endAnchor
respectively.
By default, hput
works in natural positioning mode and operates using leadingAnchor
/trailingAnchor
. This setup ensures that the layout is mirrored for Right-to-Left languages. However, this behavior can be overridden by enabling the useAbsolutePositioning
flag. When this flag is set to true, hput
shifts to using leftAnchor
/rightAnchor
for layout positioning.
A PutIntent
is essentially an instruction for calculating the width or height of:
- a spacer (for which a
UILayoutGuide
is created behind the scenes) - a view
- an array of views (when they are aligned in parallel)
Concrete instances of PutIntent
can be created using specialized builder functions:
Fix
Used for specifying the exact size of a view/spacer.
Flex
Useful for sizes that change dynamically. Optionally, it is possible to specify min/max constraints and in-place priority settings for hugging and compression resistance.
Grow
Grow
allows a view/spacer to proportionally occupy the available free space based on its weight. It's particularly useful for achieving equal spacing, centering elements, or for designing symmetrical layouts like tables or grids.
Match
This is used to match the size of a view or spacer to a specified NSLayoutDimension
. It is particularly useful for aligning the sizes of different views or spacers, or for making their sizes proportional to each other.
Examples
Fill Parent With Inset
Pin To Parent Trailing Bottom
Center In Parent
Center Label In Parent
Vertically Center Two Labels
Cell With Icon Title Subtitle And Chevron
Card With Icon Title And Subtitle
Labels Row With Not Enough Space For Both
Labels Split
Flex Min Max
Put Between Anchors
Put Between Anchors Absolute
Shadow Using Match
Integration
Use Swift Package Manager and add dependency to Package.swift
file.
Alternatively, in Xcode select File > Add Package Dependencies…
and add FixFlex repository URL:
https://github.com/psharanda/FixFlex.git