README.md in motion-kit-0.13.0 vs README.md in motion-kit-0.14.0

- old
+ new

@@ -107,11 +107,11 @@ font UIFont.fontWithName('Comic Sans', size: 24) size_to_fit # note: there are better ways to set the center, see the frame helpers below center [CGRectGetMidX(superview.bounds), CGRectGetMidY(superview.bounds)] - text_alignment UITextAlignmentCenter + text_alignment NSTextAlignmentCenter text_color UIColor.whiteColor # if you prefer to use shorthands from another gem, you certainly can! background_color rmq.color.white # from RMQ background_color :white.uicolor # from SugarCube @@ -704,10 +704,51 @@ width.equals(nth(:foo, 5)) end end ``` +One common use case is to use a child layout to create many instances of the +same layout that repeat, for instance a "row" of content. In this case you will +probably have many views with the same id, and you will not know the index of +the container view that you want to add constraints to. In this situation, use +the `nearest`, `previous` or `next` method to find a container, sibling, or +child view. + +`previous` and `next` are easy; they just search for a sibling view. No +superviews or subviews are searched. + +`nearest` will search child views, siblings, and superviews, in that order. The +"distance" is calculated as such: + +- the current view +- subviews +- siblings +- superview +- superview's siblings, or a child of the sibling (depth-first search) +- continue up the tree + +See the AutoLayout sample app for an example of this usage. + +```ruby +items.each do |item| + add UIView, :row do + add UIImageView, :avatar + add UILabel, :title + end +end + +def title_style + constraints do + # center the view vertically + center.equals(nearest(:row)) + # and place it to the right of the :avatar + left.equals(nearest(:avatar), :right).plus(8) + right.equals(nearest(:row)).minus(8) + end +end +``` + One pain point in working with constraints is determining when to add them to your views. We tried really hard to figure out a way to automatically add them, but it's just an untenable problem (Teacup suffers from a similar conundrum). Essentially, the problem comes down to this: you will often want to set @@ -729,11 +770,13 @@ width('100%') end end end - # this method will be called from `UIViewController#updateViewConstraints` + # You should call this method from `UIViewController#updateViewConstraints` + # and pass in your controller def add_constraints(controller) + # guard against adding these constraints more than once unless @layout_constraints_added @layout_constraints_added = true constraints(:label) do top.equals(controller.topLayoutGuide) end