Code Monkey home page Code Monkey logo

pin's Introduction

Features

  • iOS 12 compatible
  • Maximum readability
  • No new concepts or magic

Usage

import Pin adds new properties to Layoutable elements such as views and layout guides.

Define a constraint using operators like so:

let constraint = label.bottomPin == view.bottomPin // NSLayoutConstraint

Constants, Multipliers and Priorities

let widthConstraint = label.widthPin <= view.widthPin * 0.75 ~ UILayoutPriorityDefaultHigh
let topConstraint = label.topPin == imageView.bottomPin + 10

Convenience methods

Pin a views center. offset and multiplier is optional.

titleLabel.pinCenter(to: view) // [NSLayoutConstraint]

Pin a views edges to another view. insets is optional.

backgroundView.pinEdges(to: view) // [NSLayoutConstraint]

Pin a views edges to another views edge margins

longTextLabel.pinEdgeMargins(to: textContainer) // [NSLayoutConstraint]

Sequence of NSLayoutConstraint is extended when importing Pin with two methods.

let constraints = [
  label.centerXPin == view.centerXPin,
  label.width <= 250,
  label.topPin == view.topLayoutGuide.bottomPin
]

constraints.activate() // Activates constraints
constraints.deactivate() // Deactivates constraints

Real-world usage

You can create multiple constraints without storing them, for instance, in viewDidLoad:

override func viewDidLoad() {
	super.viewDidLoad()
	
	var constraints = [NSLayoutConstraint]()

    constraints += backgroundView.pinEdges(to: view)

    constraints += [
      titleLabel.topPin == topLayoutGuide.bottomPin + 30,

      titleLabel.centerXPin == view.centerXPin,
      titleLabel.widthPin <= view.widthPin * 0.75,

      textContainer.bottomPin == bottomLayoutGuide.topPin - 30,
      textContainer.leftPin == view.leftMarginPin,
      textContainer.rightPin == view.rightMarginPin
    ]

    constraints += longTextLabel.pinEdgeMargins(to: textContainer)

    constraints.activate()
}

You can also use NSLayoutConstraint.make for an even cleaner implementation:

NSLayoutConstraint.make { constraints in
	constraints += backgroundView.pinEdges(to: view)
	constraints.append(textLabel.width == 200)
}.activate()

Expressive syntax

If you don't fancy operators, you can use the more expressive syntax, or mix as you like:

NSLayoutConstraint.activate([
    label.topPin.equal(to: view.topMarginPin),
    label.centerXPin.equal(to: view.centerXPin),
    label.leftPin.greaterThanOrEqual(to: view.leftPin * 0.25) ~ UILayoutPriorityDefaultHigh,
    label.rightPin.lessThanOrEqual(to: view.rightPin.multiplied(by: 0.75).offset(by: 10)).prioritized(at: UILayoutPriorityDefaultHigh)
    ])

Credits

pin's People

Contributors

davidask avatar giopalusa avatar

Watchers

James Cloos 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.