From abe725f50c1929280077b515f87cdfd6d58338bb Mon Sep 17 00:00:00 2001 From: Michal Jirku Date: Fri, 26 Jul 2013 22:56:21 +0200 Subject: [PATCH 1/3] Add constraint options, with string/symbol shortcuts --- lib/motion-layout/layout.rb | 41 +++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index d8a2bc1..c64c6be 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -21,16 +21,41 @@ def view(view) @view = view end - def horizontal(horizontal) - @horizontals << horizontal + def horizontal(horizontal, *options) + options = [:centery] if options.empty? + @horizontals << [horizontal, resolve_options(options)] end - def vertical(vertical) - @verticals << vertical + def vertical(vertical, *options) + options = [:centerx] if options.empty? + @verticals << [vertical, resolve_options(options)] end private + def resolve_opts(opt) + opt_hash = { + left: NSLayoutFormatAlignAllLeft, + right: NSLayoutFormatAlignAllRight, + top: NSLayoutFormatAlignAllTop, + bottom: NSLayoutFormatAlignAllBottom, + leading: NSLayoutFormatAlignAllLeading, + trailing: NSLayoutFormatAlignAllTrailing, + centerx: NSLayoutFormatAlignAllCenterX, + centery: NSLayoutFormatAlignAllCenterY, + baseline: NSLayoutFormatAlignAllBaseline, + } + opt.inject(0) do |m,x| + if x.kind_of?(Numeric) + m | x.to_i + elsif o = opt_hash[x.to_s.downcase.to_sym] + m | o + else + raise "invalid opt: #{x.to_s.downcase}" + end + end + end + def strain @subviews.values.each do |subview| subview.translatesAutoresizingMaskIntoConstraints = false @@ -38,11 +63,11 @@ def strain end constraints = [] - constraints += @verticals.map do |vertical| - NSLayoutConstraint.constraintsWithVisualFormat("V:#{vertical}", options:NSLayoutFormatAlignAllCenterX, metrics:@metrics, views:@subviews) + constraints += @verticals.map do |vertical, options| + NSLayoutConstraint.constraintsWithVisualFormat("V:#{vertical}", options:options, metrics:@metrics, views:@subviews) end - constraints += @horizontals.map do |horizontal| - NSLayoutConstraint.constraintsWithVisualFormat("H:#{horizontal}", options:NSLayoutFormatAlignAllCenterY, metrics:@metrics, views:@subviews) + constraints += @horizontals.map do |horizontal, options| + NSLayoutConstraint.constraintsWithVisualFormat("H:#{horizontal}", options:options, metrics:@metrics, views:@subviews) end @view.addConstraints(constraints.flatten) From 8f0fdecab96929af0a424636d58a8a51e441a62d Mon Sep 17 00:00:00 2001 From: Michal Jirku Date: Fri, 26 Jul 2013 23:02:32 +0200 Subject: [PATCH 2/3] Fix typo --- lib/motion-layout/layout.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index c64c6be..b61c2d1 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -33,7 +33,7 @@ def vertical(vertical, *options) private - def resolve_opts(opt) + def resolve_options(opt) opt_hash = { left: NSLayoutFormatAlignAllLeft, right: NSLayoutFormatAlignAllRight, From 5bddcbc938502a4fcaaee30a26cfbf31a56d1651 Mon Sep 17 00:00:00 2001 From: Michal Jirku Date: Sat, 27 Jul 2013 11:07:38 +0200 Subject: [PATCH 3/3] Fix strain when @view == subview (subview centering FTW) --- lib/motion-layout/layout.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index b61c2d1..4e163b7 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -53,11 +53,12 @@ def resolve_options(opt) else raise "invalid opt: #{x.to_s.downcase}" end - end + end end def strain @subviews.values.each do |subview| + next if @view == subview # you wouldn't believe the mess it would create subview.translatesAutoresizingMaskIntoConstraints = false @view.addSubview(subview) end