Extensible extension of a Micro Auto Layout DSL

This is my first blog post ever, that’s why I would like to quickly introduce myself. My name is Adrian Śliwa and I’m an iOS applications developer. During my several years of iOS experience I always wanted to be up to date with iOS concepts. Recently I discovered an incentive inside myself to start sharing my ideas and thoughts of iOS applications development. That’s why I’m here writing my first blog post for you. I hope you will enjoy it. 
So let’s start.

On October 27, 2017 Chris Eidhof released his post about Micro Auto Layout DSL: http://chris.eidhof.nl/post/micro-autolayout-dsl/. I was really inspired by his work and I started using it in my projects because his implementation was very simple and I could manage the code by myself. His concept was based on introducing special typealias:

In his blog post he is explaining how using this simple concept and using KeyPath‘s we can achieve something like this:

Next part of the post assumes you are at least briefly familiar with Chris’es concept and his implementation. If you would like to get to know the details of it please visit his website here: http://chris.eidhof.nl/post/micro-autolayout-dsl/.

There is a limitation with the implementation of above DSL: each equal method is returning only one Constraint:

Second limit is related with overloaded addSubview implementation:

This method is accepting [Constraint]. To explain why it is a limitation I would like to share with you what I would like to achieve with this kind of DSL. I would like to express only with one helper method that I would like to pin all subview’s edges to all edges of its superview:

To enable this concept pinAllEdges function has to return list of Constraints instead of returning only one. Let’s look at pinAllEdges implementation:

As you can notice I’m adding results of equal functions. To make it possible I was not overloading + operator. To make that trick possible I used the fact that + operator is implemented for arrays. Let’s look at my equal function signature:

This equal function is returning [Constraint]. Because of that fact I can mix results of my helper functions with results of equal function:

To make it all working overloaded addSubview method has to be modified a little:

Now addSubview accepts [[Constraint]] instead of [Constraint]. It’s creating actual NSLayoutConstraints and flattening list of lists to just a list of NSLayoutConstraint and at the end it’s activating all of constraints.

This powerful trick enables you to introduce your own helper layout methods. It’s very flexible and because of that fact I called it extensible extension. Let’s say you would like to have a helper to pin subview to the center of its parent. It’s very easy to add using extended version of Micro Auto Layout DSL:

It’s wonderful, isn’t it?

The whole implementation of my improvement can be found here: https://gist.github.com/adiki/6cc4e3b16b3f933f7181c76bc6a52a8d

That’s it! I hope you enjoyed it ;)

My github: https://github.com/adiki/

Next week: Simple trick to improve handling cells in UITableView and UICollectionView