From a0e783bbb00e457e8f349ebfd7585f13cd89e15d Mon Sep 17 00:00:00 2001 From: Jamon Holmgren Date: Mon, 6 May 2013 20:54:59 -0700 Subject: [PATCH] Added an method for adding any view to any other parent view with attributes. Closes issue #18. --- README.md | 15 ++++++++++++++ .../screen_helpers/screen_elements.rb | 20 ++++++++++--------- spec/screen_helpers_spec.rb | 12 +++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bc90486..c972750 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,7 @@ Run `rake`. You should now see the simulator open with your home screen and a na * Added `open_split_screen` for iPad-supported apps (thanks @rheoli for your contributions to this) * `ProMotion::AppDelegateParent` renamed to `ProMotion::Delegate` (`AppDelegateParent` is an alias) +* Added `add_to` method for adding views to any parent view. `remove` works with this normally. # Usage @@ -329,6 +330,8 @@ Any view item (UIView, UIButton, custom UIView subclasses, etc) can be added to `add` accepts a second argument which is a hash of attributes that get applied to the element before it is dropped into the view. +`add(view, attr={})` + ```ruby @label = add UILabel.alloc.initWithFrame(CGRectMake(5, 5, 20, 20)), { text: "This is awesome!", @@ -342,12 +345,24 @@ dropped into the view. The `set_attributes` method is identical to add except that it does not add it to the current view. +`set_attributes(view, attr={})` + ```ruby @element = set_attributes UIView.alloc.initWithFrame(CGRectMake(0, 0, 20, 20)), { backgroundColor: UIColor.whiteColor } ``` +You can use `add_to` to add a view to any other view, not just the main view. + +`add_to(parent_view, new_view, attr={})` + +```ruby +add_to @some_parent_view, UIView.alloc.initWithFrame(CGRectMake(0, 0, 20, 20)), { + backgroundColor: UIColor.whiteColor +} +``` + ## Table Screens You can create sectioned table screens easily with TableScreen, SectionedTableScreen, and GroupedTableScreen. diff --git a/lib/ProMotion/screen_helpers/screen_elements.rb b/lib/ProMotion/screen_helpers/screen_elements.rb index 019cb67..4055850 100644 --- a/lib/ProMotion/screen_helpers/screen_elements.rb +++ b/lib/ProMotion/screen_helpers/screen_elements.rb @@ -2,22 +2,24 @@ module ProMotion module ScreenElements include ProMotion::ViewHelper - def add(v, attrs = {}) - if attrs && attrs.length > 0 - set_attributes(v, attrs) - end - self.view.addSubview(v) - v + def add(element, attrs = {}) + add_to self.view, element, attrs end alias :add_element :add alias :add_view :add - def remove(v) - v.removeFromSuperview - v = nil + def remove(element) + element.removeFromSuperview + element = nil end alias :remove_element :remove alias :remove_view :remove + + def add_to(parent_element, element, attrs = {}) + set_attributes(element, attrs) if attrs && attrs.length > 0 + parent_element.addSubview element + element + end def bounds return self.view.bounds diff --git a/spec/screen_helpers_spec.rb b/spec/screen_helpers_spec.rb index cc13c14..4edca1c 100644 --- a/spec/screen_helpers_spec.rb +++ b/spec/screen_helpers_spec.rb @@ -24,6 +24,18 @@ describe "screen helpers" do @screen.view.subviews.count.should == 0 end + it "should add a subview to another element" do + sub_subview = UIView.alloc.initWithFrame CGRectZero + @screen.add_to @subview, sub_subview + @subview.subviews.include?(sub_subview).should == true + end + + it "should add a subview to another element with attributes" do + sub_subview = UIView.alloc.initWithFrame CGRectZero + @screen.add_to @subview, sub_subview, { backgroundColor: UIColor.redColor } + @subview.subviews.last.backgroundColor.should == UIColor.redColor + end + end