Created
July 10, 2013 15:17
-
-
Save bobmoff/5967180 to your computer and use it in GitHub Desktop.
Stack subviews vertically ordered by their index. Good when u want to use XIB's and need to layout views (show/hide) based on external data, but cannot use autolayout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface UIView (Stacker) | |
- (void)stackSubviews; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "UIView+Stacker.h" | |
@implementation UIView (Stacker) | |
- (void)stackSubviews { | |
// Init Y position of the first visible subview contained inside the StackerView | |
float _y = 0; | |
for (UIView *subview in self.subviews) { | |
// Start by adjusting the Y position of the current subview | |
subview.y = _y; | |
// If the subview is visible, then add height to the global Y position, which is | |
// used to set the Y position of the next subview in the iteration | |
if (!subview.hidden) { | |
// If the subview is a TableView, we use the contentsize instead of the frames height | |
if ([subview isMemberOfClass:[UITableView class]]) { | |
UITableView *tview = (UITableView *)subview; | |
_y += tview.contentSize.height; | |
} else { | |
_y += subview.frame.size.height; | |
} | |
// Increase the height of acctual StackerView with each visible sub-view | |
self.height = _y; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment