• Stars
    star
    555
  • Rank 80,213 (Top 2 %)
  • Language
    Swift
  • License
    ISC License
  • Created over 8 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swift microframework for declaring Auto Layout constraints functionally

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.

More Repositories

1

Barista

A modular, embeddable web server for Objective-C.
Objective-C
500
star
2

XPCKit

XPC simplified for Cocoa. Deal with NS* objects instead of xpc_object_t.
Objective-C
251
star
3

DerpKit

Objective-C categories and subclasses of things that should be in Foundation and other frameworks
Objective-C
137
star
4

IIIAsync

Concurrency control flow system for Objective-C
Objective-C
113
star
5

DevCenter.me

Directory of developer center websites with memorable URL shortcuts
CSS
80
star
6

awesome-ios-widgets

A curated list of home screen widgets for apps on iOS 14+
74
star
7

VillainousStyle

A standalone Mac/iPhone port of the TTStyle and TTShape classes from the Three20 project
Objective-C
70
star
8

EasyTweet

A simple Objective-C wrapper for the Twitter API with no OAuth dependencies
Objective-C
60
star
9

CrashReporter

Send iOS crash reports by email
C
57
star
10

url-shrink

URL Shrink provides a system-wide utility to shorten and expand URLs across multiple services (is.gd, bit.ly, etc.)
Objective-C
47
star
11

sscore

Cocoa code I use just about everywhere.
Objective-C
36
star
12

better-xcode-templates

A package of better Xcode templates for iPhone and Mac
Objective-C
34
star
13

CoreFoundation

A git-based mirror of the official CoreFoundation code
C
29
star
14

Swearch

An iOS-optimized web app for accessing search engines
JavaScript
29
star
15

PlayerPiano

A Pianobar GUI for Mac OS X Snow Leopard
C
29
star
16

oauthery

Tool for manually logging into Twitter using OAuth, with Cocoa code demonstrating the OAuth login process for Twitter
Objective-C
27
star
17

MWNetworking

Right now, one Objective-C class to download a thing. Eventually, DOWNLOAD ALL THE THINGS.
Objective-C
23
star
18

BlockKit

YOU get a block, YOU get a block, EVERYONE gets a block!
Objective-C
20
star
19

caboose

A Mac client for getting push notifications from the Boxcar service
C
16
star
20

TOML

Kind-of-works TOML parser for Objective-C
Objective-C
12
star
21

AppApp

AppApp is an iOS app for http://app.net. It was hastily copy pasted together at 3am.
Objective-C
11
star
22

TDAppKit

Git mirror of TDAppKit from @itod
Objective-C
11
star
23

EmbedMonster

Universal (eventually) audio/video controller JavaScript API for embeddable media
JavaScript
9
star
24

MiniPiano

An iPhone app which plays music from Pandora
Objective-C
7
star
25

html5playlist

A simple web app to play a playlist
JavaScript
7
star
26

node-pop3

A POP3 library for node.js. Maybe it'll work one day.
6
star
27

technicolor

Media organization tool for Mac OS X
Objective-C
6
star
28

fightclub

Cocoa framework for connecting to Hellanzb servers
Objective-C
6
star
29

Colloquy

Git port of the Colloquy IRC client for Mac
Objective-C
6
star
30

hal-project

Home automation project written in Python for Arduino boards (SVN mirror)
Python
6
star
31

lessn-service

OS X Service for shortening a URL using your installation of Lessn/ButteredURLs
Objective-C
6
star
32

ZNC-Node

A module for ZNC to add node.js-based modules
C++
4
star
33

flash-machine

Cocoa application to host SWF files and bookmarks to SWFs
Objective-C
4
star
34

technicolor-networking

A set of networking plugins for Technicolor, including a shared HTTP server
Objective-C
3
star
35

bmo

Plugin-based XMPP bot
JavaScript
3
star
36

NudeWiki

A wiki, built with Node.js and MongoDB, because that's what you build when learning new server-side technologies
JavaScript
2
star
37

facebook-iphone-sdk

Facebook Connect for iPhone
Objective-C
2
star
38

dock-killer

OS X app for killing the dock via the menu bar
Objective-C
2
star
39

technicolor-file-importer

A simple plugin for Technicolor to demonstrate importing files into records
Objective-C
2
star
40

StatsKit

An open source analytics system for developers on any platform. In development.
JavaScript
2
star
41

turntable.userscripts.fm

Various userscripts for turntable.fm
JavaScript
2
star
42

csh-drink-iphone

iPhone client for the CSH Drink machines
2
star
43

node-pq

A Redis-backed priority queue for node.js.
JavaScript
2
star
44

dotfiles

Various dotfiles
Shell
2
star
45

technicolor-fuse

A FUSE plugin for accessing files stored in Technicolor
Objective-C
2
star
46

Boxque

A node.js-based server framework for queueing Boxcar notifications through Resque
JavaScript
2
star
47

node-boxcar

A node.js Connector for Boxcar
1
star
48

bitlbee

Mirror of the Bitlbee project, with some of my own changes
C
1
star
49

InformalProtocolTheme

Theme for InformalProtocol.com
JavaScript
1
star
50

CoreMustache

C
1
star
51

js-calendar-picker

A small calendar picker written with jQuery which reveals itself as needed.
1
star