Latest 0.8
License MIT
Platforms ios 9.0
Dependencies Texture

ASDKFluentExtensions is a set of Objective-C categories that extends Texture with a fluent interface for layout code. This fluent style is more visual, improves readability (less visual scanning to grasp layout structure), and makes layout more concise and easier to maintain. It also enables composition of layout specs and styling.

Imagine you want to overlay an image with a gradient. Then overlay the gradient with a title that must be positioned in the bottom right corner of the gradient, with some insets. Finally, the whole thing should have an aspect ratio of 1. With ASDKFluentExtensions you can write layout code like this:

override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
  return imageNode
        .withRelativePosition(horizontal: .end, vertical: .end)
        .withInset(UIEdgeInsetsMake(.infinity, .infinity, 4, 4))))

Both ASDisplayNode and ASLayoutSpec subclasses can be wrapped within an ASLayoutSpec using this fluent syntax. Just start typing .with and look for the appropriate completion:

autocompletion with

All ASDKFluentExtensions methods return an object conforming to ASLayoutElement, so fluent layouts can be chained together. Furthermore, modifications of the style property can also be composed with layout definitions. This way the information flows in a meaningful order. Look at the modifications of flexGrow and alignSelf in the following example:

func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
  return ASStackLayoutSpec
    .withInset(UIEdgeInsets(top: 60, left: 0, bottom: 60, right: 0))

ASDKFluentExtensions also provide categories over ASStackLayoutSpec and ASAbsoluteLayoutSpec which makes it possible to inline these specs in layout code with a very readable style:

func fluentLayoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
  return ASAbsoluteLayoutSpec()
        .withPreferredSize(CGSize(width: 150, height: 150))
        .withLayoutPosition(CGPoint(x: 40 / 2.0, y: 40 / 2.0)),
        .withPreferredSize(CGSize(width: 40, height: 40))
        .withLayoutPosition(CGPoint(x: 150, y: 0))])

There is even a category to conveniently create spacers!

let header = ASStackLayoutSpec().withChildren([userName, ASLayoutSpec.spacer(), lastTimeOnline])

Finally, this is a more contrived example, the layout code of PhotoTableNodeCell from the well known ASDKgram-Swift example rewritten to use fluent layout:

func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
  return ASStackLayoutSpec
            .withPreferredSize(CGSize(width: Constants.CellLayout.UserImageHeight, height: Constants.CellLayout.UserImageHeight))



This layout reads from top to bottom without interruptions. The outermost code define the general structure, and inner parts define the details. The information flows in a meaningful order, so less visual scanning is needed. Also, it is much faster to add, remove, and reorganize specs, for example during debugging or prototyping. Finally, note how modifications of style properties are composable with layout specs (see usages of withPreferredSize or withFlexShrink inline with layout specs definition). This avoids interruptions on how the code visually flows.

More About Fluent Interfaces

If you want to know more about Fluent API’s, make sure to check this paper in which Martin Fowler and Eric Evans coined the term.


There are a several examples on how to use ASDKFluentExtensions to write fluent layout code available in this fork of Texture. They are clones of existing examples included in the main Texture repo, modified to illustrate ASDKFluentExtensions. The idea is that people wanting to approach this fluent syntax can have a set of familiar examples to look at.

This is the list of currently available examples:


ASDKFluentExtensions currently support Cocoapods and Carthage.


You can install ASDKFluentExtensions using CocoaPods. Add the pod to your Podfile:

target 'MyApp' do
  pod "ASDKFluentExtensions"

Then run the following command:

$ pod install

Make sure to import the header:

#import <ASDKFluentExtensions/ASDKFluentExtensions.h>


From version 0.6 you can build ASDKFluentExtensions using Carthage. Add the following line to your Cartfile:

github "cesteban/ASDKFluentExtensions" >= 0.6

Then build the framework:

carthage update

Finally, add the generated framework to your project as usual.


ASDKFluentExtensions are just a bunch of Objective-C categories over ASDisplayNode, ASLayoutSpec, ASStackLayoutSpec, and ASAbsoluteLayoutSpec that create and return the desired layout spec passing self as child:

- (ASOverlayLayoutSpec *)withOverlay:(id<ASLayoutElement>)overlay
  return [ASOverlayLayoutSpec overlayLayoutSpecWithChild:self overlay:overlay];

ASDKFluentExtensions also offer categories to modify style properties of the ASDisplayNode or ASLayoutSpec returning self:

- (instancetype)withPreferredSize:(CGSize)preferredSize
{ = preferredSize;
    return self;

This makes layout and styling composable, and enables a very readable fluent API.

Code Duplication and Swift Protocol Extensions

There is a lot of code duplication in order to provide the same functionality for both ASDisplayNode and ASLayoutSpec.

This could be avoided in Swift using protocol extensions over ASLayoutElement, but then the API won’t be available from Objective-C. On the other hand, Texture is a pure Objective-C framework, so extending it in Swift would introduce limitations. Check here the original Swift implementation of ASDKFluentExtensions, very early abandoned in favor of the current Objective-C API.


Send all the feedback you have, and please contribute. Pull Requests and suggestions on how to improve this component are very much appreciated.

Latest podspec

    "name": "ASDKFluentExtensions",
    "version": "0.8",
    "license": {
        "type": "MIT"
    "homepage": "",
    "authors": {
        "Cesar Estebanez": "[email protected]"
    "summary": "Extend AsyncDisplayKit with a fluent layout API",
    "source": {
        "git": "",
        "tag": "0.8"
    "platforms": {
        "ios": "9.0"
    "public_header_files": "Source/*.h",
    "source_files": "Source/*.{h,m}",
    "dependencies": {
        "Texture": [
            "~> 2.6"

Pin It on Pinterest

Share This