This edition of iOS Open Source is a category extension to the UIView class that adds code to facilite managing UIView hieracrhies. The code offers a straight-forward means to manage where views appear in a subview hierachy. If you are unfamiliar with Objective-C categories, head over here: Working with Categories.
The code below shows the category definition and the methods defined in UIView+Hierarchy.h.
@interface UIView (Hierarchy) -(int)getSubviewIndex; -(void)bringToFront; -(void)sendToBack; -(void)bringOneLevelUp; -(void)sendOneLevelDown; -(BOOL)isInFront; -(BOOL)isAtBack; -(void)swapDepthsWithView:(UIView*)swapView; @end
Once you create a class with a UIView and import UIView+Hierarchy.h you can easily request views to be moved to forward and backward in the hierachy as well as inquire if a view is at the front or back.
UIView* greenView; [greenView bringOneLevelUp]; [greenView sendOneLevelDown]; if ([greenView isInFront] == YES) ...
This category and sample project was written by Marin Todorov. Thanks Marin for making this code open source.
Source Code Download
The UIView Category source code and an Xcode project to exercise the code can be downloaded here: ViewSortDemo.zip
Suggestions For Open Source Projects?
If you have written, worked with or otherwise know of an interesting open source project, one that is focused on iOS development, feel free to send me a note. Even a simple class/category that offers something useful is welcome. Contact me with ideas and suggestions.