Giter Club home page Giter Club logo

uikitlayout's Introduction

UIKitLayout

UIKitLayout provides syntactic sugar on top of the NSLayoutAnchor system to facilitate easy to setup, easy to debug, and easy to adjust auto-layout constraints.

It allows two views to "pin" themselves to one another in a syntax that reads like english:

let first = UIView()
let second = UIView()

first.trailing.pin(to: second.leading) // H:[first]-[second]

Add spacing between two pinned axes:

first.trailing.pin(to: second.leading, spacing: 5) // H:[first]-(5)-[second]

Add a relation other than equals for the constraint:

first.trailing.pin(to: second.leading, .greaterThanOrEqual, spacing: 5)  // H:[first]-(>=5)-[second]

This works with view dimensions as well as axis:

first.width.pin(to: second.height)

And instead of spacing, you can set a ratio:

first.width.pin(to: second.height, ratio: Ratio(1, to: 2))  // width of first will be half the height of second

And/or add a constant:

first.width.pin(to: second.height, plus: 5) // first's width will be 5 points greater than second's height

You'll be stopped from doing things that don't make sense:

first.width.pin(to second.leading) // compiler error
first.trailing.pin(to: second.bottom) // compiler error

Add a subview to a contrainer and pin all edges in once call:

let parent = UIView()
let child = UIView()
parent.embed(child)

Or specific anchors:

parent.embed(child, pin: [.top, .bottom, .centerX])

Adding offset:

parent.embed(child, pin: [.top(offset: 5), .bottom(offset: -10), .centerX(offset: -4)])

With relations besides equals:

parent.embed(child, pin: [.top(offset: 5), .bottom(.greaterThanOrEqual, offset: -10), .centerX(offset: -4)])
  • All embed functions will turn off the child view's translatesAutoresizingMaskIntoConstraints property.

Of course setting an explicit height or width is easy:

first.height.pin(to: 5)
first.width.pin(to: 6)

As is setting the aspect ratio:

first.constrain(aspectRatio: .equal)

All calls return the constraints they create:

let constraint = first.width.pin(to: second.height)

All calls accept activate as the last parameter:

let uninstalledConstraint = first.constrain(aspectRatio: .equal, activate: false)

And priority as the second to last parameter:

let unrequiredConstraint = first.constrain(aspectRatio: .equal, priority: .defaultLow)

Pinnable anchors include the system provided layout guides:

first.safeAreaLayoutGuide.trailing.pin(to: second.layoutMarginsGuide.Leading)

But your own layout guide may be used as well:

let guide: UILayoutGuide
guide.leadingAnchor.pin(to: .trailing, of: first)
guide.widthAnchor.pin(to: guide.heightAnchor)

Happy Coding! And check out the Grid class which uses UIKitLayout to create an n x m grid with variable row heights and column widths.

uikitlayout's People

Contributors

pm-dev avatar

Watchers

 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.