Swift microframework for declaring Auto Layout constraints functionally

Related tags

Layout Relayout
Overview

Relayout

video.png

https://img.shields.io/travis/stevestreza/Relayout/master.svg?maxAge=2592000

https://camo.githubusercontent.com/3dc8a44a2c3f7ccd5418008d1295aae48466c141/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43617274686167652d636f6d70617469626c652d3442433531442e7376673f7374796c653d666c6174

https://img.shields.io/cocoapods/v/Relayout.svg?maxAge=2592000

https://img.shields.io/cocoapods/p/Relayout.svg?maxAge=2592000

https://img.shields.io/cocoapods/metrics/doc-percent/Relayout.svg?maxAge=2592000

Relayout is a Swift microframework to make using Auto Layout easier with static and dynamic layouts.

Why?

If you want to build a UI using Apple's UI frameworks today, you have three good options. You can use Auto Layout in Interface Builder, you can use Auto Layout in code and maintain references to those constraints, or you can implement a layout function with layoutSubviews. Each of these approaches has pros and cons.

  • If you layout manually with layoutSubviews, you're usually doing the job of Auto Layout - describing the layout of UI elements. But instead of doing it with objects to describe those relationships, you end up describing it with mathematical equations. This can be the most explicit and least error prone, but can be difficult to read and understand later. You also don't pick up features like right-to-left language support. But you get the benefit of having a single method that handles all your layout, regardless of why a layout changed.
  • If you use Auto Layout in Interface Builder, you can end up with inconsistencies in your actual layout vs what Interface Builder creates, as it tries to magically fix issues with your layout. It can also be difficult to change those constraints later, either at runtime, or when you need to add a new feature. However, if you need a simple UI that doesn't change much, it can be an effective and quick way to get the job done.
  • If you use Auto Layout in code, you get the benefits of the higher level abstraction and improved code readability. And this can be a good approach if you don't need dynamism in your UI. But if you need to be able to change the UI at runtime, you have to hold on to references to individual constraints, add and remove constraints, and hope you don't see the dreaded "Unable to simultaneously satisfy constraints" warning.

The fundamental problem behind making dynamic layouts is state transformations. Going from 1 state to 2 is easy (add 2 transformations, A -> B and B -> A). Going from 2 to 3 is still fairly easy (add 4 transformations, B -> C, A -> C, C -> B, and C -> A). But as you add more and more states, you end up doubling the number of transformations you need to account for, all while fully satisfying the constraint system. This exponential growth is unsustainable, especially when you don't really care about the transformations as much as the states themselves.

Wouldn't it be great if we could cherry pick some of the most useful properties of the approaches? Auto Layout is a very good tool because of how descriptive it is, and we should use it, but not in its current form. Let's take a page from React.js and define a function that returns a pile of constraints that we want for a given UI state.

You could define a single object that generates a pile of constraints for your view. Any time anything happens that could possibly change those constraints, throw the old ones out and generate a new set of constraints. That's what Relayout is.

Since the goal of Relayout is to make it easier to use Auto Layout, it tries to have a minimal impact on your app. You can use it in a view controller or within individual views. You can use it for parts of your app, and not use it elsewhere. You can compose layouts together and control them conditionally based on behaviors like UITraitCollection state. Whatever you want to do.

What It Is Not

Relayout is not a tool for creating constraints. That's up to you. Want to use the NSLayoutConstraint visual formatting language? The iOS 9+ anchor APIs? A third-party library like PureLayout? As long as it returns NSLayoutConstraint objects, Relayout can use them. But we're not going to be opinionated about how you create them, or whether they come pre-activated or not.

Relayout is also not an alternative or replacement to Auto Layout, but rather an augmentation. It requires the use of Auto Layout, and that means the rules that come along with it. So if you hate Auto Layout and don't want to use it, then Relayout may not be for you (though you may find that the reasons you hate Auto Layout no longer exist when using Relayout!).

Relayout is also not the definitive implementation of a functional layout. It's an idea, that having a function that generates a list of constraints is a good way to build UI. It's a set of objects that implement that idea. Feel free to take this idea, and build on it, to help make it easier to build and scale UIs.

Finally, Relayout is not a great tool to use in conjunction with Interface Builder. Both Interface Builder and Relayout want you to supply a complete set of constraints to fully describe a UI. Trying to get those two to play nicely is a fight neither you nor I want to solve.

Installing

Relayout supports iOS 8.0+, tvOS 9.0+, and OS X 10.10+. You can install Relayout with Carthage, CocoaPods, or manually.

With CocoaPods

Add the following to your Podfile.

pod 'Relayout', '~> 1.1'

With Carthage

Add the following to your Cartfile.

github "stevestreza/Relayout" ~> 1.1

Manually

Relayout.xcodeproj creates a Relayout.framework, so you can include the framework as an Xcode target dependency, and copy/link it into your app. Alternatively, you can include the source files from the Framework target as you see fit.

Swift 3

When Swift 3 lands the project will be immediately updated to support it with a major update to the version number (e.g. version 2.0.0).

Usage

You can use Relayout from within both views and view controllers. To take advantage of it, you will need a ViewLayout object. Create one with a root view (such as the view controller's view, the table cell's content view, or the view itself). You will also need to give it an object that conforms to the LayingOut protocol. This protocol is very simple and has one method which takes your root view and returns an Array<NSLayoutConstraint>, which represents the list of constraints that you want applied.

Once you have a ViewLayout object, you'll want to call its layout() method anywhere that might trigger a layout. Places where that may happen include updateConstraints, setFrame, traitCollectionDidChange, and any place where your UI's state changes.

The simplest way to return constraints is to use the Layout object, which accepts either an Array<NSLayoutConstraint> to pass through directly, or a closure that takes your root view and returns the Array<NSLayoutConstraint> to apply to it.

Relayout was designed to be composable, meaning that the Layout object is one building block to use to build powerful and flexible layouts. There are a number of implementations of the LayingOut protocol, either existing or planned. So far you can use:

  • LayoutGroup, which returns all of the NSLayoutConstraint objects provided by an Array<LayingOut> object
  • IdentifyingLayout, which adds an identifier to all NSLayoutConstraint objects for a given LayingOut object (which is useful for debugging unsatisfiable constraint errors)
  • ConditionalLayout, which returns the NSLayoutConstraint objects from a given LayingOut object if the condition is true, and optionally return other NSLayoutConstraint objects if the condition is false
  • TraitCollectionLayout, which returns the NSLayoutConstraint objects from a given LayingOut object iff the root view has certain UITraitCollection traits
  • ListLayout, which iterates over a list of objects, calling a closure that returns NSLayoutConstraint objects when passed the object, its index, and the previous and next objects in the list (to easily constrain between an object and its next and previous views).

And you can of course implement the LayingOut protocol if you see fit. It has no Self requirement, so you can use them interchangeably anywhere.

You might also like...
An Impressive Auto Layout DSL for  iOS, tvOS & OSX. & It is written in pure swift.
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

KVConstraintKit KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift Installation Using CocoaPods

A compact but full-featured Auto Layout DSL for Swift
A compact but full-featured Auto Layout DSL for Swift

Mortar allows you to create Auto Layout constraints using concise, simple code statements. Use this: view1.m_right |=| view2.m_left - 12.0 Instead of:

The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible.
The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible.

The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. PureLayout extends UIView/NSView, NSArray, and NSLayoutConstrai

Auto Layout In Swift Made Easy

Swiftstraints Swiftstraints can turn verbose auto-layout code: let constraint = NSLayoutConstraint(item: blueView, attr

Yet Another Swift Auto Layout DSL
Yet Another Swift Auto Layout DSL

FormationLayout Documentation FormationLayout is the top level layout class for one root view. FormationLayout takes a UIView as its rootView. transla

Lightweight declarative auto-layout framework for Swift

SwiftyLayout SwiftyLayout is a framework that allows to describe layout constraints (ie NSLayoutConstraint) as a simple mathematical formula in a Swif

Minimal Auto Layout in Swift
Minimal Auto Layout in Swift

Restraint Restraint is a very very small library to help make your use of NSLayoutConstraint in Swift more legible & declarative. Like programmatic vi

SuperLayout is a Swift library that makes using Auto Layout a breeze.
SuperLayout is a Swift library that makes using Auto Layout a breeze.

SuperLayout is a library that adds a few custom operators to Swift that makes using the amazing NSLayoutAnchor API for Auto Layout a breeze. SuperLayo

Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.

QuickLayout QuickLayout offers an additional way, to easily manage the Auto Layout using only code. You can harness the power of QuickLayout to align

Comments
  • Update multiplatform support with universal target

    Update multiplatform support with universal target

    • Builds a universal framework for macOS, iOS, and tvOS using Relayout-specific xcconfig files, which are based on https://github.com/mrackwitz/xcconfigs but disable watchOS support.
    • Uses a single test suite for all supported platforms
    • CI commands all work individually on my machine ¯\_(ツ)_/¯
    opened by asmallteapot 2
  • Add full support for tvOS and macOS

    Add full support for tvOS and macOS

    Current version has framework support for iOS, and Cocoapods support for iOS and tvOS. This should support framework targets for tvOS and macOS. Similarly add testing to them and integrate that testing with Travis CI.

    opened by stevestreza 2
  • LICENSE: add file extension

    LICENSE: add file extension

    Since this file is not hard-wrapped, the extension is needed to trigger github's soft-wrapping rendering mode. This also allows the title to be highlighted with the appropriate semantic markup (a markdown heading).

    opened by waldyrious 0
  • Is this project still maintained ?

    Is this project still maintained ?

    Hello there,

    I've been wanting to try out this library but realized it's not compatible with Swift 3+.

    It seems that not only it really solves a problem that no other library does, but it also does it in an elegant way. It would be a shame if it rots away :-( . What are the plans for this project ?

    By the way, I love the introduction video, very original !

    Thanks

    opened by rz-robsn 1
Releases(1.1.0)
Owner
Steve Streza
Comic book author and iOS developer.
Steve Streza
UIView category which makes it easy to create layout constraints in code

FLKAutoLayout FLKAutoLayout is a collection of categories on UIView which makes it easy to setup layout constraints in code. FLKAutoLayout creates sim

Florian Kugler 1.5k Nov 24, 2022
In this repository I've learned how to use the layout as well as alignment and constraints.

Auto Layout Our Goal At the moment, our app only looks good on the canvas dimension that we’ve selected. If you run the app on screens with different

Pedro Couventaris Daspett 1 Apr 23, 2022
Auto Layout (and manual layout) in one line.

Auto Layout (and manual layout) in one line. Quick Look view.bb.centerX().below(view2).size(100) It’s equivalent to iOS 9 API: view.centerXAnchor.cons

Javier Zhang 74 Oct 19, 2022
Auto Layout made easy with the Custom Layout.

Auto Layout made easy with the Custom Layout. Getting started CocoaPods CocoaPods is a dependency manager for Cocoa projects. You can install it with

Malith Nadeeshan 1 Jan 16, 2022
Minimal AutoLayout convenience layer. Program constraints succinctly.

MiniLayout Minimal AutoLayout convenience layer. Program constraints succinctly. Usage Put label over textField // using MiniLayout: view.constrain(la

Yonat Sharon 8 Jul 7, 2021
iOS constraint maker you always wanted. Write constraints like sentences in English. Simple

YeahLayout iOS constraint maker you always wanted. Write constraints like sentences in English. Simple. Intuitive. No frightening abstractions. One fi

Андрей Соловьев 1 Jan 10, 2022
Build 1 scene, let AutoLayoutMagic generate the constraints for you!

Auto Layout Magic Create 1 scene, let Auto Layout Magic generate the constraints for you Hello friends, We've all been there. You have an app supporti

Matt 61 Sep 21, 2019
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]

Extremely Fast views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainabl

layoutBox 2.1k Dec 22, 2022
A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

Cartography ?? ?? Using Cartography, you can set up your Auto Layout constraints in declarative code and without any stringly typing! In short, it all

Robb Böhnke 7.3k Jan 4, 2023
Lightweight Swift framework for Apple's Auto-Layout

I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API. A

null 349 Dec 20, 2022