Code Monkey home page Code Monkey logo

restraint's Introduction

Restraint

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

  • Like programmatic views?
  • Like the benefits of using pure AutoLayout?
  • Like clear and minimal interfaces?
  • Dislike Visual Format Language and "stringly" typing?
  • Dislike the verbosity of NSLayoutConstraint?
  • Dislike heavy dependencies?
  • Practice Restraint!

Carthage

Features

  • As simple as possible
  • Easy to maintain
  • Easy to replace
  • Easy to circumvent
  • Not too clever
  • Sane defaults
  • Automatic handling of setTranslatesAutoresizingMaskIntoConstraints

Basic Example

Let's set the height & width of an imageView to 200 points and center in the current UIView. Here's how we would do that with NSLayoutConstraint:

let imageViewWidthConstraint = NSLayoutConstraint(
  item:       imageView,
  attribute:  .Height,
  relatedBy:  .Equal,
  toItem:     nil,
  attribute:  .NotAnAttribute,
  multiplier: 1.0,
  constant:   200
)

let imageViewHeightConstraint = NSLayoutConstraint(
  item:       imageView,
  attribute:  .Width,
  relatedBy:  .Equal,
  toItem:     nil,
  attribute:  .NotAnAttribute,
  multiplier: 1.0,
  constant:   200
)

imageView.addConstraints([imageViewWidthConstraint, imageViewHeightConstraint])

let imageViewHorizontalConstraint = NSLayoutConstraint(
  item:       imageView,
  attribute:  .CenterX,
  relatedBy:  .Equal,
  toItem:     self,
  attribute:  .CenterX,
  multiplier: 1.0,
  constant:   0
)

let imageViewVerticalConstraint = NSLayoutConstraint(
  item:       imageView,
  attribute:  .CenterY,
  relatedBy:  .Equal,
  toItem:     self,
  attribute:  .CenterY,
  multiplier: 1.0,
  constant:   0
)

addConstraints([imageViewHorizontalConstraint, imageViewVerticalConstraint])

๐Ÿ˜จ

And of course, this depends upon your particular formatting conventions. You know how quickly this gets out of hand.

Here's how to apply these same constraints with Restraint:

Restraint(imageView, .Width,  .Equal, 200).addToView(imageView)
Restraint(imageView, .Height, .Equal, 200).addToView(imageView)

Restraint(imageView, .CenterX, .Equal, self, .CenterX).addToView(self)
Restraint(imageView, .CenterY, .Equal, self, .CenterY).addToView(self)

๐Ÿ’†

Detailed Example

Example View

For this view, we'd like to:

  • Center the imageView (which is of a fixed size) vertically and horizontally in the containing view.
  • Center topLabel (which is of a variable size) vertically between the top of imageView and the top of the containing view.
  • Center topLabel horizontally in the containing view.
  • Center bottomLabel (which is of a variable size) vertically between the bottom of imageView and the bottom of the containing view.
  • Set the width of bottomLabel to the width of the containing view less its layout margins.

Here are those rules expressed with Restraint:

// Image View Constraints

Restraint(imageView, .Width,  .Equal, imageViewSize).addToView(self)
Restraint(imageView, .Height, .Equal, imageViewSize).addToView(self)

Restraint(imageView, .CenterX, .Equal, self, .CenterX).addToView(self)
Restraint(imageView, .CenterY, .Equal, self, .CenterY).addToView(self)

// Top Label Constraints

Restraint(topLabel, .CenterX, .Equal, imageView, .CenterX).addToView(self)
Restraint(topLabel, .CenterY, .Equal, imageView, .Top, 0.5, 0).addToView(self)

// Bottom Label Constraints

Restraint(bottomLabel, .Left,  .Equal, self, .LeftMargin).addToView(self)
Restraint(bottomLabel, .Right, .Equal, self, .RightMargin).addToView(self)

Restraint(bottomLabel, .CenterY, .Equal, self, .CenterY, 1.5, (200 / 4)).addToView(self)

You can see this in action in Example/View.swift

How It Works

Each Restraint simply creates the appropriate NSLayoutConstraint, and the call to addToView disables translatesAutoresizingMaskIntoConstraints on the left and right views in the constraint and adds that constraint to the target view.

Optionally, you can call Restraint().constraint() to get an instance of NSLayoutConstraint and add your constraint with UIView#addConstraint or #addConstraints later. For example:

let heightConstraint = Restraint(imageView, .Height, .Equal, 200).constraint()

imageView.addConstraint(heightConstraint)

In this case you'll need to handle disabling translatesAutoresizingMaskIntoConstraints yourself.

Installation

Carthage

Add the following to your project's Cartfile:

github "puffinsupply/Restraint" >= 0.0.1

Manual

Simply add Restraint.swift to your project.

License

restraint's People

Contributors

soopa avatar

Watchers

 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.