GithubHelp home page GithubHelp logo

gillfrost / anchorchain Goto Github PK

View Code? Open in Web Editor NEW
3.0 0.0 0.0 232 KB

A fluent API for constraint-based iOS layout.

License: MIT License

Ruby 0.84% Swift 99.16%
ios swift autolayout constraints auto layout constraint anchor chainable dsl

anchorchain's Introduction

AnchorChain

Build Status codecov Version Platform License

A wrapper around the NSLayoutAnchor system, driven by the following set of design goals / trade-offs:

  • Ease of expression should be proportional to frequency of use. In other words: constraining a view to its superview's edges should be trivial, and indeed it is: view.anchor()

  • Intuitiveness at the point of use. Constraining a view's top to the bottom of another view should probably be expressed as: view.anchor(.top, to: .bottom, of: anotherView), and indeed it is.

    • Trade-off: autocompletion will be of limited use, as the anchor method is heavily overloaded.
  • Chainability to promote a little more declarative layout code. Constraining a view at its declaration point should be as easy as let view = UIView().anchoring(.width, to: 100), and indeed it is.

  • Proactiveness with the view hierarchy. When constraining anchors to match another view, such as this: UIActivityIndicatorView().anchor(.centerX, .centerY, to: view), it could be assumed that the receiver, if it is without superview, should be added as subview to the other view, and indeed it is.

    • Trade-off: it's a pretty severe side effect, but it makes sense, since creating (activated) constraints between two views with no common ancestor constitutes a programmer error anyway.

Usage

Matching attributes

...to superview

// All edges
view.anchor()

// Some edges
view.anchor(.top, .left, .right)

// To safe area
view.anchor(to: .safeArea)

// With insets
view.anchor(with: UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20))

// Save constraint for later use
let constraint = view.anchor(.top)

// Inactive constraint
let constraint = view.anchor(.top, isActive: false)

// Prioritized like a complete refactoring in your backlog
view.anchor(.top, priority: .defaultLow)

...to other view

// All edges
view.anchor(to: otherView)

// Some edges
view.anchor(.top, .left, .right, to: otherView)

// To safe area
view.anchor(to: .safeArea, of: otherView)

// With insets
view.anchor(to: otherView, with: UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20))

// Save constraint for later use
let constraint = view.anchor(.top, to: otherView)

// Inactive constraint
let constraint = view.anchor(.top, to: otherView, isActive: false)

// With priority
view.anchor(.top, to: otherView, priority: .defaultLow)

Sizing

view.anchor(.width, to: 100)
view.anchor(.height, to: 100)
// or simply
view.anchor(.size, to: 100)

Alignment

// Top to bottom of other view
view.anchor(.top, to: .bottom, of: otherView)

// Left greater than or equal to right of other view
view.anchor(.left, .greaterThanOrEqual, to: .right, of: otherView)

Requirements

iOS 11

XCode 10

Swift 4.2

Installation

CocoaPods

To integrate AnchorChain to your XCode project using CocoaPods, add the following to your Podfile:

pod 'AnchorChain'

License

AnchorChain is available under the MIT license. See the LICENSE file for more info.

anchorchain's People

Contributors

andregillfrost avatar gillfrost avatar

Stargazers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.