hkellaway / Autolycus

Autolayout DSL :triangular_ruler:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Autolycus 📐

Swift version Carthage compatible License Build Status

Autolayout DSL for writing layout and constraints programmatically.

Getting Started

  • Download Autolycus and do a pod install on the included AutolycusDemo app to see Autolycus in action

Installation with CocoaPods

pod 'Autolycus', :git => 'git@github.com:hkellaway/Autolycus.git', :tag => '0.2.0'

Installation with Carthage

github "hkellaway/Autolycus"

Usage

Autolycus is simply syntactic sugar around the verbose programmatic layout API.

Example

Let's look at a simple example of aligning two views next to each other.

With Autolycus 📐:

firstView.constrain().size(of: secondView)
firstView.constrain().top(to: secondView)
firstView.constrain().leadingToTrailing(of: secondView)

A bit more verbose with NSLayoutDimension (iOS 9.0+):

firstView.translatesAutoresizingMaskIntoConstraints = false
firstView.widthAnchor.constraint(equalTo: secondView.heightAnchor).isActive = true
firstView.heightAnchor.constraint(equalTo: secondView.heightAnchor).isActive = true
firstView.topAnchor.constraint(equalTo: secondView.topAnchor).isActive = true
firstView.leadingAnchor.constraint(equalTo: secondView.trailingAnchor).isActive = true

Clunkiest of all, with NSLayoutConstraint:

firstView.translatesAutoresizingMaskIntoConstraints = false
let width = NSLayoutConstraint(item: firstView, attribute: .width, relatedBy: .equal, toItem: secondView, attribute: .width, multiplier: 1, constant: 0)
let height = NSLayoutConstraint(item: firstView, attribute: .height, relatedBy: .equal, toItem: secondView, attribute: .height, multiplier: 1, constant: 0)
let leadingToTrailing = NSLayoutConstraint(item: firstView, attribute: .leading, relatedBy: .equal, toItem: secondView, attribute: .trailing, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([width, height, top, leadingToTrailing])

The purpose of Autolycus is to simplify constraint creation and obfuscate odd API details.

Autolycus follows the usage passing of calling constrain() on the view adopting programmatic layout then chaining together a list of desired constraints, which are automatically applied.

Convenience functions

Here is a list of convneience functions that Autolycus provides. Each of these produces one or more constraints that are applied to views called upon.

Note that these functions can be called with various parameters to specify placement, such as offeset or insets.

Constraints are activated by default, but can be made inactive by providing the isActive parameter as false.

For single view

  • width (e.g. view.constrain().width(10))
  • height
  • size

For relationship between two views

  • width(to view:) (e.g. view.constrain().width(to view: otherView))
  • height(to view:)
  • centerX(to view:)
  • centerY(to view:)
  • leading(to view:)
  • left(to view:)
  • trailing(to view:)
  • right(to view:)
  • top(to view:)
  • bottom(to view:)
  • inCenter(of view:)
  • edges(to view:)
  • size(of view:)
  • origin(to view:)
  • leadingToTrailing(of view:)
  • leftToRight(of view:)
  • trailingToLeading(of view:)
  • rightToLeft(of view:)
  • topToBottom(of view:)
  • bottomToTop(of view:)

For relationship to superview

  • widthToSuperview (e.g. view.constrain().widthToSuperView())
  • heightToSuperview
  • centerXToSuperview
  • centerYToSuperview
  • leadingToSuperview
  • leftToSuperview *trailingToSuperview
  • rightToSuperview
  • topToSuperview
  • bottomToSuperview
  • centerInSuperview
  • edgesToSuperview
  • sizeOfSuperview
  • originToSuperview

Discussion

There is nothing fancy going on here. Autolycus simply creates NSLayoutConstraint instances in a more literate way with reasonable defaults. It also obfuscates the oft-forgotten necessity of setting translatesAutoresizingMaskIntoConstraints = false and isActive = true.

To that point, our original example could be written more manually with Autolycus just used to generate constraints:

firstView.translatesAutoresizingMaskIntoConstraints = false
        
let constraintsArray: [NSLayoutConstraint] = firstView.size(of: secondView, isActive: false)
    + [firstView.top(to: secondView, isActive: false)]
    + [firstView.leadingToTrailing(of: secondView, isActive: false)]
        
NSLayoutConstraint.activate(constraintsArray)

Because Autolycus only creates NSLayoutConstraint instances, these can be stored for later usage to take advantage of animation or hiding/moving views by way of constraint manipulation.

Credits

Autolycus was created by Harlan Kellaway.

License

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

About

Autolayout DSL :triangular_ruler:

License:MIT License


Languages

Language:Swift 98.9%Language:Ruby 0.6%Language:Objective-C 0.5%