Added an method for adding any view to any other parent view with attributes. Closes issue #18.

This commit is contained in:
Jamon Holmgren
2013-05-06 20:54:59 -07:00
parent d96cf40625
commit a0e783bbb0
3 changed files with 38 additions and 9 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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