diff --git a/motion/ruby_motion_query/position.rb b/motion/ruby_motion_query/position.rb index c8f52cc..affddb7 100644 --- a/motion/ruby_motion_query/position.rb +++ b/motion/ruby_motion_query/position.rb @@ -106,16 +106,16 @@ module RubyMotionQuery self end - def resize_frame_to_fit_subviews(padding = {}) + def resize_frame_to_fit_subviews(args = {}) selected.each do |view| - view.rmq.layout(subviews_bottom_right(view, padding)) + view.rmq.layout(subviews_bottom_right(view, args)) end self end alias :resize_to_fit_subviews :resize_frame_to_fit_subviews - def resize_content_to_fit_subviews(padding = {}) + def resize_content_to_fit_subviews(args = {}) selected.each do |view| unless view.respond_to?(:contentSize) puts "\n[RMQ ERROR] resize_content_to_fit_subviews must be called on a view that supports `contentSize`. Called on #{view}\n\n" @@ -123,7 +123,7 @@ module RubyMotionQuery end view.rmq.style do |st| - bottom_right = subviews_bottom_right(view, padding) + bottom_right = subviews_bottom_right(view, args) st.content_size = CGSizeMake(bottom_right[:w], bottom_right[:h]) end end @@ -151,7 +151,7 @@ module RubyMotionQuery # Calculates the bottom right of a view using its subviews # # @return [Hash] - def subviews_bottom_right(view, padding = {}) + def subviews_bottom_right(view, args = {}) w = 0 h = 0 @@ -162,10 +162,10 @@ module RubyMotionQuery end rect = view.rmq.frame - w = rect.width if w == 0 - h = rect.height if h == 0 + w = rect.width if (w == 0 || args[:only_height]) + h = rect.height if (h == 0 || args[:only_width]) - {w: (w + (padding[:right] || 0)), h: (h + (padding[:bottom] || 0))} + {w: (w + (args[:right] || 0)), h: (h + (args[:bottom] || 0))} end end diff --git a/spec/position.rb b/spec/position.rb index 455e4a7..d762813 100644 --- a/spec/position.rb +++ b/spec/position.rb @@ -127,26 +127,92 @@ describe 'position' do view.size.height.should == 100 # Just right + view.rmq.layout(h:100, w:20) view.rmq.resize_frame_to_fit_subviews({right: 101}) view.size.width.should == 171 view.size.height.should == 500 # Just bottom + view.rmq.layout(h:100, w:20) view.rmq.resize_frame_to_fit_subviews({bottom: 13}) view.size.width.should == 70 view.size.height.should == 513 # Both right & bottom + view.rmq.layout(h:100, w:20) view.rmq.resize_frame_to_fit_subviews({right: 101, bottom: 13}) view.size.width.should == 171 view.size.height.should == 513 # Negative values + view.rmq.layout(h:100, w:20) view.rmq.resize_frame_to_fit_subviews({right: -1, bottom: -1}) view.size.width.should == 69 view.size.height.should == 499 end + it "should resize a view's frame without changing height if specified" 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 + + view.rmq.resize_frame_to_fit_subviews({only_height: true}) + view.size.width.should == 20 + view.size.height.should == 500 + + # With bottom padding + view.rmq.layout(h:100, w:20) + view.rmq.resize_frame_to_fit_subviews({only_height: true, bottom: 10}) + view.size.width.should == 20 + view.size.height.should == 510 + + # With right padding + view.rmq.layout(h:100, w:20) + view.rmq.resize_frame_to_fit_subviews({only_height: true, right: 10}) + view.size.width.should == 30 + view.size.height.should == 500 + + # With both padding + view.rmq.layout(h:100, w:20) + view.rmq.resize_frame_to_fit_subviews({only_height: true, bottom:21, right: 10}) + view.size.width.should == 30 + view.size.height.should == 521 + end + + it "should resize a view's frame without changing width if specified" 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 + + view.rmq.resize_frame_to_fit_subviews({only_width: true}) + view.size.width.should == 70 + view.size.height.should == 100 + + # With bottom padding + view.rmq.layout(h:100, w:20) + view.rmq.resize_frame_to_fit_subviews({only_width: true, bottom: 10}) + view.size.width.should == 70 + view.size.height.should == 110 + + # With right padding + view.rmq.layout(h:100, w:20) + view.rmq.resize_frame_to_fit_subviews({only_width: true, right: 10}) + view.size.width.should == 80 + view.size.height.should == 100 + + # With both padding + view.rmq.layout(h:100, w:20) + view.rmq.resize_frame_to_fit_subviews({only_width: true, bottom:21, right: 10}) + view.size.width.should == 80 + view.size.height.should == 121 + end + it "should set a UIScrollView's contentSize property automatically" do view = @vc.rmq.append(UIScrollView).layout(h: 100, w: 20).get view.rmq.append(UIButton).layout(h: 50, w: 10)