Latest 1.1.0
License MIT
Platforms ios 9.0, osx 10.11, tvos 9.0


Carthage Compatible
CocoaPods Compatible
Swift 4
Build Status

Auto Layout (and manual layout) in one line.

Quick Look

It’s equivalent to iOS 9 API:

view.centerXAnchor.constraint(equalTo: view.superview!.centerXAnchor).isActive = true
view.topAnchor.constraint(equalTo: view2.bottomAnchor).isActive = true
view.widthAnchor.constraint(equalToConstant: 100).isActive = true
view.heightAnchor.constraint(equalToConstant: 100).isActive = true

As you can see, Bamboo eliminated a lot of redundant code.

  • Using chaining style, you can write view’s name just once.
  • All methods are grouped in bb extension, so their names can be simple and short.
  • Superview and anchor are usually implied.
  • More higher level methods like below() and size() make things easier.



github "wordlessj/Bamboo" ~> 1.0


pod 'Bamboo', '~> 1.0'



All basic anchors on UIView and UILayoutGuide are provided with suffix Anchor stripped, e.g., left instead of leftAnchor.

// view.left == superview.left

// Or use it on UILayoutGuide.

To specify another view’s corresponding anchor, simply pass the view to the method.

// view1.left == view2.left

Of course, you can use a specific anchor.

// view1.left == view2.right

Including a constant is straightforward using operator + or -.

// view1.left == view2.right + 10 + 10)

// Specific view or anchor can be omitted.
// view.left == superview.left + 10

For dimension anchors, when only a constant is specified, it sets the dimension to the constant, instead of matching to superview.

// view.width == 10

width() and width(0) are different, the former match to superview and the latter set to 0.

In iOS 11, you can use SystemSpacing in place of constant, pass multiplier to the initializer, only + operator is supported.

// view1.left == view2.right + 2 * SystemSpacing + SystemSpacing(2))

Similarly, use >= or <= to specify equality.

// view1.left >= view2.right + 10>=view2.rightAnchor + 10)

To specify priority, use operator ~.

// view1.left == view2.right + 10 (priority 500) + 10 ~ 500)

For dimension anchors, multiplier can be specified with * or /.

// view1.width == 2 * view2.width * view2)


Expressions are passed to basic anchor methods. Full form:

( >= or <= ) item + constant ~ priority

For dimension anchors:

( >= or <= ) item * multiplier + constant ~ priority

item can be UIView, UILayoutGuide or NSLayoutAnchor. You can use / and - instead of * and +. If equality is not specified, multiplier can be put before item to be more like linear functions. More complex expressions can be created, but for simplicity, it’s not recommended.


You can get a single constraint created from the method for later use.

let c: NSLayoutConstraint =

Or get all constraints accumulated from the chain.

let c: [NSLayoutConstraint] =

Activate and Deactivate

Constraints can be activated and deactivated easily so you can better control the layout change.

// Deactivate after creation.
let c: [NSLayoutConstraint] =

// Activate later when appropriate.

Higher Level Methods

Aspect Ratio

// view.width == 2 * view.height

Center and Size

Both methods are similar to basic anchor methods.

  • center(), consists of centerX and centerY.
  • size(), consists of width and height.

There are two more methods to set size.

let cgSize: CGSize 10, height: 20)


// Pin all edges.

// Pin corresponding edge and adjacent edges.
// e.g., fillLeft() pin left, top and bottom.

// Pin edges on corresponding axis.
// e.g., fillWidth() pin leading and trailing.

All fill methods take two optional arguments, first is either UIView or UILayoutGuide to be filled, nil for its superview, second is a UIEdgeInsets.

// view1 fills view2 with insets.
let insets: UIEdgeInsets, insets: insets)

// If all edge insets are the same, you can pass a single value.
// view1 fills view2 with insets 10., insets: 10)


  • before()
  • after()
  • above()
  • below()

These methods take two optional arguments like fill methods, except the second one is spacing.

// view1.trailing == view2.leading - 10, spacing: 10)


In iOS 10, use - operator to create an offset anchor between two axis anchors, then use offset() to create a constraint. It does not matter which view is used before bb.

// view2.left - view1.right == view3.left - view2.right - view1.rightAnchor, view3.leftAnchor - view2.rightAnchor)

Multiple Items

You can constrain on multiple items at once. There are two fundamental methods on which other methods are built.

// Constrain on each item.
// e.g., Set each item's width to 10.
[view1, view2, view3].bb.each {

// Constrain between every two items.
// e.g., view1.left == view2.left, view2.left == view3.left
[view1, view2, view3].bb.between {

Most basic methods on single item are available on multiple items.

// Anchor methods take no arguments and align all items' corresponding anchor.
// e.g., Align all views' left.
[view1, view2, view3].bb.left()

// For dimension anchors, you can set values for all items.
// e.g., Set all views' width to 10.
[view1, view2, view3].bb.width(10)

You can distribute items along an axis with fixed spacing or equal spacing.

  • distributeX()
  • distributeY()
  • distributeXEqualSpacing()
  • distributeYEqualSpacing()
// [view1]-10-[view2]-10-[view3]
[view1, view2, view3].bb.distributeX(spacing: 10)

All distribute methods come with an optional inset parameter, you can specify no insets, fixed insets or equal insets.

// |-[view1]-[view2]-[view3]-|, spacings are all equal.
[view1, view2, view3].bb.distributeXEqualSpacing(inset: .equal)

Manual Layout

Why manual layout when there is Auto Layout? Well, Auto Layout is good, but it has some performance issues, that’s when you may want to switch to manual layout.

Manual Layout Guide


Panda is a framework which creates view hierarchies declaratively, together with Bamboo, they make creating views in code incredibly simple and easy.


Bamboo is released under the MIT license. See LICENSE for details.

Latest podspec

    "name": "Bamboo",
    "version": "1.1.0",
    "summary": "Auto Layout (and manual layout) in one line.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": "Javier Zhang",
    "source": {
        "git": "",
        "tag": "1.1.0"
    "platforms": {
        "ios": "9.0",
        "osx": "10.11",
        "tvos": "9.0"
    "source_files": "Source/**/*.swift",
    "pushed_with_swift_version": "4.0"

Pin It on Pinterest

Share This