From ad0464430b999aa10c1276a5bd6f8c84af1fd8a6 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Fri, 20 Feb 2015 11:18:21 -0600 Subject: [PATCH] Allows you to specify right and bottom padding for resize_to_fit_subviews --- motion/ruby_motion_query/position.rb | 4 ++-- spec/position.rb | 29 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/motion/ruby_motion_query/position.rb b/motion/ruby_motion_query/position.rb index 6c77d2f..f44a5ff 100644 --- a/motion/ruby_motion_query/position.rb +++ b/motion/ruby_motion_query/position.rb @@ -106,7 +106,7 @@ module RubyMotionQuery self end - def resize_to_fit_subviews + def resize_to_fit_subviews(padding = {}) selected.each do |view| w = 0 h = 0 @@ -121,7 +121,7 @@ module RubyMotionQuery w = rect.width if w == 0 h = rect.height if h == 0 - view.rmq.layout(w: w, h: h) + view.rmq.layout(w: (w + (padding[:right] || 0)), h: (h + (padding[:bottom] || 0))) end self diff --git a/spec/position.rb b/spec/position.rb index 47f8ab3..0fa0079 100644 --- a/spec/position.rb +++ b/spec/position.rb @@ -118,6 +118,35 @@ describe 'position' do view.size.height.should == 500 end + it 'should resize to fit subviews with padding' do + view = @vc.rmq.append(UIView).layout(h: 100, w: 20).get + view.rmq.append(UIButton).layout(h: 50, w: 10) + view.rmq.append(UILabel).layout(h: 500, w: 70) + view.rmq.append(UIView).layout(h: 5, w: 1) + view.size.width.should == 20 + view.size.height.should == 100 + + # Just right + view.rmq.resize_to_fit_subviews({right: 101}) + view.size.width.should == 171 + view.size.height.should == 500 + + # Just bottom + view.rmq.resize_to_fit_subviews({bottom: 13}) + view.size.width.should == 70 + view.size.height.should == 513 + + # Both right & bottom + view.rmq.resize_to_fit_subviews({right: 101, bottom: 13}) + view.size.width.should == 171 + view.size.height.should == 513 + + # Negative values + view.rmq.resize_to_fit_subviews({right: -1, bottom: -1}) + view.size.width.should == 69 + view.size.height.should == 499 + end + it 'should nudge a view in various directions' do view = @vc.rmq.append(UILabel).get view.origin.x.should == 0