Skip to content

floriankrueger/Manuscript

Repository files navigation

GitHub release CircleCI Carthage compatible CocoaPods Swift GitHub license Gitmoji

Manuscript: Dead-Simple AutoLayout

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

Features

  • concise, simple and convenient API
  • raw AutoLayout power
  • no black magic involved
  • fully documented
  • 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.

Carthage

To integrate Manuscript into your Xcode project using Carthage, specify it in your Cartfile:

github "floriankrueger/Manuscript"

If you need to support Swift 2.3 then please use the last compatible version 2.1.0

github "floriankrueger/Manuscript" == 2.1.0

If you need to support Swift 3.0 then please use the last compatible version 3.0.1

github "floriankrueger/Manuscript" == 3.0.1

If your Swift 3.1 compiler isn't compatible with the framework binary from the github release then please use the following command to build Manuscript yourself:

carthage bootstrap Manuscript --no-use-binaries

CocoaPods

Make sure your Podfile contains all of the following lines.

use_frameworks!
platform :ios, '8.0'

pod 'Manuscript'

Then run pod install.

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.