Auto Layout (and manual layout) in one line.

Overview

Bamboo


Carthage Compatible CocoaPods Compatible Swift 4.2 Platform Build Status

Auto Layout (and manual layout) in one line.

Quick Look

view.bb.centerX().below(view2).size(100)

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.

Installation

Carthage

github "wordlessj/Bamboo" ~> 1.0

CocoaPods

pod 'Bamboo', '~> 1.0'

Usage

Basics

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

// view.left == superview.left
view.bb.left()

// Or use it on UILayoutGuide.
layoutGuide.bb.left()

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

// view1.left == view2.left
view1.bb.left(view2)

Of course, you can use a specific anchor.

// view1.left == view2.right
view1.bb.left(view2.rightAnchor)

Including a constant is straightforward using operator + or -.

// view1.left == view2.right + 10
view1.bb.left(view2.rightAnchor + 10)

// Specific view or anchor can be omitted.
// view.left == superview.left + 10
view.bb.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
view.bb.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
view1.bb.left(view2.rightAnchor + SystemSpacing(2))

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

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

To specify priority, use operator ~.

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

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

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

Expression

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.

Constraints

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

let c: NSLayoutConstraint = view.bb.left().constraint

Or get all constraints accumulated from the chain.

let c: [NSLayoutConstraint] = view.bb.left().top().constraints

Activate and Deactivate

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

// Deactivate after creation.
let c: [NSLayoutConstraint] = view.bb.left().top().constraints.deactivate()

// Activate later when appropriate.
c.activate()

Higher Level Methods

Aspect Ratio

// view.width == 2 * view.height
view.bb.aspectRatio(2)

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
view.bb.size(cgSize)

view.bb.size(width: 10, height: 20)

Fill

// Pin all edges.
fill()

// Pin corresponding edge and adjacent edges.
// e.g., fillLeft() pin left, top and bottom.
fill[Left|Right|Top|Bottom|Leading|Trailing]()

// Pin edges on corresponding axis.
// e.g., fillWidth() pin leading and trailing.
fill[Width|Height]()

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
view1.bb.fill(view2, insets: insets)

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

Around

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

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

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

Offset

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.bb.offset(view2.leftAnchor - 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 {
    $0.bb.width(10)
}

// Constrain between every two items.
// e.g., view1.left == view2.left, view2.left == view3.left
[view1, view2, view3].bb.between {
    $0.bb.left($1)
}

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)

Group

If you want to activate/deactivate a set of constraints in response to some events, you can use group() to collect constraints into an array.

let constraints: [NSLayoutConstraint] = group {
    view1.bb.fill()
    view2.bb.left().top().size(100)
    view3.bb.fillBottom().height(100)
}

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

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

License

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

You might also like...
Auto Layout made easy
Auto Layout made easy

EasyPeasy is a Swift framework that lets you create Auto Layout constraints programmatically without headaches and never ending boilerplate code. Besi

Lightweight Swift framework for Apple's Auto-Layout
Lightweight Swift framework for Apple's Auto-Layout

I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API. A

An Impressive Auto Layout DSL for  iOS, tvOS & OSX. & It is written in pure swift.
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

KVConstraintKit KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift Installation Using CocoaPods

A compact but full-featured Auto Layout DSL for Swift
A compact but full-featured Auto Layout DSL for Swift

Mortar allows you to create Auto Layout constraints using concise, simple code statements. Use this: view1.m_right |=| view2.m_left - 12.0 Instead of:

Auto Layout In Swift Made Easy

Swiftstraints Swiftstraints can turn verbose auto-layout code: let constraint = NSLayoutConstraint(item: blueView, attr

TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use.
TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use.

TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use. Features Pure Swift 5 sweetness. Everything you can do with Auto

⚓️ Declarative, extensible, powerful Auto Layout
⚓️ Declarative, extensible, powerful Auto Layout

EasyAnchor ❤️ Support my apps ❤️ Push Hero - pure Swift native macOS application to test push notifications PastePal - Pasteboard, note and shortcut m

Template auto layout cell for automatically UITableViewCell height calculating
Template auto layout cell for automatically UITableViewCell height calculating

UITableView-FDTemplateLayoutCell Overview Template auto layout cell for automatically UITableViewCell height calculating. Basic usage If you have a se

Yet Another Swift Auto Layout DSL
Yet Another Swift Auto Layout DSL

FormationLayout Documentation FormationLayout is the top level layout class for one root view. FormationLayout takes a UIView as its rootView. transla

Comments
  • Did I misunderstand the usage of Bamboo?

    Did I misunderstand the usage of Bamboo?

    view.pd.add(scrollView.pd.backgroundColor(UIColor.red)) scrollView.pd.contentOffset(CGPoint(x: 0, y: -300)).object.bb.fill() scrollView.pd.add(

    categoryButtonFactory().bb.width(115).height(92).center().item.pd.title("title1") .action(action: { b in }).backgroundColor(UIColor.red),

    categoryButtonFactory().bb.width(115).height(92).centerX(-115).centerY().item.pd.title("title2") .action(action: { b in }).backgroundColor(UIColor.gray),

    categoryButtonFactory().bb.width(115).height(92).centerX(115).centerY().item.pd.title("title3") .action(action: { b in }).backgroundColor(UIColor.green) )}

    image
    opened by ZZHHAANNGG 2
Releases(1.2.0)
  • 1.2.0(Oct 5, 2018)

  • 1.1.0(Sep 16, 2017)

    Additions

    • Support system spacing in iOS 11, use SystemSpacing in place of constant.
    • Support offset anchors in iOS 10, use offset() and - operator for axis anchors.
    • distribute[X|Y]EqualSpacing() distribute items with equal spacing.
    • triple() on multiple items creates constraints for every three items.

    Changes

    • Migrate to Swift 4.
    • constrain and layout extensions are renamed to bb and bbm respectively.
    • before() and after() respect language direction.
    • distribute[Horizontally|Vertically] are renamed to distribute[X|Y], additional inset parameter is added.
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Jul 6, 2017)

Owner
Javier Zhang
iOS developer, ❤️ UI design.
Javier Zhang
Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout

Masonry Masonry is still actively maintained, we are committed to fixing bugs and merging good quality PRs from the wider community. However if you're

null 18k Jan 5, 2023
Using the UIKitChain framework, You can create a UIKit component in one line of code.

Using the UIKitChain framework, You can create a UIKit component in one line of code. Installation CocoaPods CocoaPods is a dependency manager for Coc

Malith Nadeeshan 1 Sep 1, 2022
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

Extremely Fast views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainabl

layoutBox 2.1k Dec 22, 2022
The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible.

The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. PureLayout extends UIView/NSView, NSArray, and NSLayoutConstrai

PureLayout 7.6k Jan 6, 2023
Intuitive and powerful Auto Layout library

Align introduces a better alternative to Auto Layout anchors. Semantic. Align APIs focus on your goals, not the math behind Auto Layout constraints. P

Alexander Grebenyuk 338 Oct 18, 2022
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.

QuickLayout QuickLayout offers an additional way, to easily manage the Auto Layout using only code. You can harness the power of QuickLayout to align

Daniel Huri 243 Oct 28, 2022
Declarative Auto Layout in Swift, clean and simple

Tails Tails is a take on declarative Auto Layout. If you don't like typing (like me), it might be your kind of thing! Tails is written in Swift and cu

Nick Tymchenko 17 Jan 31, 2019
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast

Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

layoutBox 2.1k Jan 2, 2023
Very simple swipe-to-dismiss, supporting Auto Layout and dynamic heights

PanelPresenter Add swipe-dismiss logic to your view controller, supporting Auto Layout and dynamic heights. Installation Add this package to your proj

Pim 3 Aug 23, 2022
A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

Cartography ?? ?? Using Cartography, you can set up your Auto Layout constraints in declarative code and without any stringly typing! In short, it all

Robb Böhnke 7.3k Jan 4, 2023