Manuscript 3.0.3

Manuscript 3.0.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Apr 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Florian Krüger.



Manuscript: Dead-Simple AutoLayout

It’s like AutoLayoutKit but written in Swift. For pure Swift projects. And it’s super simple.

Features

  • [x] concise, simple and convenient API
  • [x] raw AutoLayout power
  • [x] no black magic involved
  • [x] fully documented
  • [x] completely unit-tested

Have a look at the Changelog for more details.

Requirements

  • iOS 8.0+
  • Xcode 8.x
  • Swift 3.1

Bonus: Support for iOS 7.0+ and/or tvOS

Although the cocoapods isn’t able to install Manuscript for your iOS 7.0+ app or for your tvOS app (yet), you can use still Manuscript. Just follow the ‘manual’ installation or add Manuscript as a framework project to your workspace (that’s what I do).

What it looks like

Manuscript.layout(myButton) { c in
  c.set(.Height, to: 60.0)
  c.set(.Width, to: 60.0)
  c.make(.Bottom, equalTo: self.view, s: .Bottom, minus: 10.0)
  c.make(.CenterX, equalTo: self.view, s: .CenterX)
}

Usage

A few examples on how to use Manuscript.

Center and set size

Center a UIView 'childView’ in self.view and make it 30 by 30 in size

Manuscript.layout(childView) { c in
  c.make(.CenterX, equalTo: view, s: .CenterX)
  c.make(.CenterY, equalTo: view, s: .CenterY)
  c.set(.Width, to: 30.0)
  c.set(.Height, to: 30.0)
}

The same, but using the convenience methods

Manuscript.layout(childView) { c in
  c.centerIn(view)
  c.setSize(CGSize(width: 30.0, height: 30.0))
}

Align all edges of a view to a superview

Align a UIView 'container’ to all edges of self.view

Manuscript.layout(container) { c in
  c.make(.Left, equalTo: view, s: .Left)
  c.make(.Right, equalTo: view, s: .Right)
  c.make(.Top, equalTo: view, s: .Top)
  c.make(.Bottom, equalTo: view, s: .Bottom)
}

The same, but using the convenience methods

Manuscript.layout(container) { c in
  c.alignAllEdges(to: view)
}

Align all sides with insets

Align a UIView 'container’ to all edges of self.view and leave a 30 point margin around the container.

Manuscript.layout(container) { c in
  c.make(.Left, equalTo: view, s: .Left, plus: 30.0)
  c.make(.Right, equalTo: view, s: .Right, minus: 30.0)
  c.make(.Top, equalTo: view, s: .Top, plus: 30.0)
  c.make(.Bottom, equalTo: view, s: .Bottom, minus: 30.0)
}

The same, but using convenience methods.

Manuscript.layout(container) { c in
  c.alignAllEdges(to: view, withInsets: UIEdgeInsets(top: 30.0, left: 30.0, bottom: 30.0, right: 30.0))
}

Use LayoutGuides

Make use of the LayoutGuides provided by UIViewController.

Manuscript.layout(container) { c in
  c.make(.Left, equalTo: view, s: .Left)
  c.make(.Right, equalTo: view, s: .Right)
  c.make(.Top, equalTo: topLayoutGuide, s: .Baseline)
  c.make(.Bottom, equalTo: bottomLayoutGuide, s: .Baseline)
}

Hairlines

There is a utility method to create hairlines which takes the screen scale into account.

Manuscript.layout(mySeparatorLine) { c in
  c.make(.Left, equalTo: view, s: .Left)
  c.make(.Right, equalTo: view, s: .Right)
  c.make(.Top, equalTo: topLayoutGuide, s: .Baseline)

  // sets the .Height to 1.0 on non-retina displays and to 0.5 on retina displays
  c.makeHorizontalHairline()
}

Work with the created Layout Constraints

The functions make and set return a tuple of type LayoutItem which translates to (constraint: NSLayoutConstraint?, targetItem: UIView?). The 'constraint’ is the created NSLayoutConstraint, the 'targetItem’ is the view to which the NSLayoutConstraint was added. It is the nearest common superview of the UIViews involved.

Manuscript.layout(container) { c in
  leftConstaint = c.make(.Left, equalTo: view, s: .Left).constraint
  rightConstaint = c.make(.Right, equalTo: view, s: .Right).constraint
  topConstaint = c.make(.Top, equalTo: topLayoutGuide, s: .Baseline).constraint
  bottomConstaint = c.make(.Bottom, equalTo: bottomLayoutGuide, s: .Baseline).constraint
}

Afterwards, just modify the constraint’s constant and apply the changes (this is plain AutoLayout).

UIView.animateWithDuration(0.6) { in
  topConstraint?.constant = 100
  view.layoutIfNeeded()
}

Convenience Methods

The convenience methods return arrays of the mentioned tuples. These will be dictionaries or tuples in the future as well to provide easier access to the created constraints. Until then, check the code for the order of the returned constraints.

Installation

As for now, you can use Carthage or CocoaPods to install Manuscript using a dependency manager or do it manually.

Manually

To do it 'by hand’ take the following files and add them to your project:

  • Source/Manuscript.swift
  • Source/LayoutProxy.swift

License

Manuscript is released under the MIT License.