Skip to content

Commit

Permalink
Add support for tabBarItems with different heights.
Browse files Browse the repository at this point in the history
  • Loading branch information
robbdimitrov committed Aug 18, 2013
1 parent 15adfd4 commit 2f7b200
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
18 changes: 14 additions & 4 deletions RDVTabBarController/RDVTabBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,26 @@ @interface RDVTabBar ()
@implementation RDVTabBar

- (void)layoutSubviews {
CGSize frameSize = self.frame.size;

if ([self edgeContentInset]) {
[self setItemWidth:(CGRectGetWidth(self.frame) - 2 * [self edgeContentInset]) / [[self items] count]];
[self setItemWidth:(frameSize.width - 2 * [self edgeContentInset]) / [[self items] count]];
} else {
[self setItemWidth:CGRectGetWidth(self.frame) / [[self items] count]];
[self setEdgeContentInset:CGRectGetWidth(self.frame) - [[self items] count] * [self itemWidth]];
[self setItemWidth:frameSize.width / [[self items] count]];
[self setEdgeContentInset:frameSize.width - [[self items] count] * [self itemWidth]];
}

for (NSInteger i = 0; i < [[self items] count]; i++) {
RDVTabBarItem *item = [[self items] objectAtIndex:i];
[item setFrame:CGRectMake(self.edgeContentInset + (i * self.itemWidth), 0, self.itemWidth, CGRectGetHeight(self.frame))];

CGFloat itemHeight = [item itemHeight];

if (!itemHeight) {
itemHeight = frameSize.height;
}

[item setFrame:CGRectMake(self.edgeContentInset + (i * self.itemWidth), roundf(frameSize.height - itemHeight),
self.itemWidth, itemHeight)];
}
}

Expand Down
17 changes: 15 additions & 2 deletions RDVTabBarController/RDVTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ - (void)viewWillLayoutSubviews {
}
}

[[self contentView] setFrame:CGRectMake(0, 0, viewSize.width, viewSize.height - tabBarHeight)];
[[self contentView] setFrame:CGRectMake(0, 0, viewSize.width, viewSize.height - [self minimumTabBarContentHeight])];
[[self tabBar] setFrame:CGRectMake(0, viewSize.height - tabBarHeight, viewSize.width, tabBarHeight)];
}

Expand Down Expand Up @@ -166,7 +166,7 @@ - (NSInteger)indexForViewController:(UIViewController *)viewController {
- (RDVTabBar *)tabBar {
if (!_tabBar) {
_tabBar = [[RDVTabBar alloc] init];
[_tabBar setBackgroundColor:[UIColor lightGrayColor]];
[_tabBar setBackgroundColor:[UIColor clearColor]];
[_tabBar setDelegate:self];
}
return _tabBar;
Expand All @@ -180,6 +180,19 @@ - (UIView *)contentView {
return _contentView;
}

- (CGFloat)minimumTabBarContentHeight {
CGFloat minimumTabBarContentHeight = CGRectGetHeight([self tabBar].frame);

for (RDVTabBarItem *item in [[self tabBar] items]) {
CGFloat itemHeight = [item itemHeight];
if (itemHeight && (itemHeight < minimumTabBarContentHeight)) {
minimumTabBarContentHeight = itemHeight;
}
}

return minimumTabBarContentHeight;
}

#pragma mark - RDVTabBarDelegate

- (BOOL)tabBar:(RDVTabBar *)tabBar shouldSelectItemAtIndex:(NSInteger)index {
Expand Down
5 changes: 5 additions & 0 deletions RDVTabBarController/RDVTabBarItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
- (UIImage *)finishedUnselectedImage;
- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage;

/**
* itemHeight is an optional parameter. When set it is used instead of tabBar's height
*/
@property CGFloat itemHeight;

@end

0 comments on commit 2f7b200

Please sign in to comment.