Skip to content

Commit

Permalink
Delegated size of tabBar-item
Browse files Browse the repository at this point in the history
  • Loading branch information
myell0w committed Apr 24, 2012
1 parent 35f8ed2 commit 19054e2
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 29 deletions.
Expand Up @@ -44,5 +44,20 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

////////////////////////////////////////////////////////////////////////
#pragma mark - NGTabBarControllerDelegate
////////////////////////////////////////////////////////////////////////

- (CGSize)tabBarController:(NGTabBarController *)tabBarController
sizeOfItemForViewController:(UIViewController *)viewController
atIndex:(NSUInteger)index
position:(NGTabBarPosition)position {
if (NGTabBarIsVertical(position)) {
return CGSizeMake(150.f, 60.f);
} else {
return CGSizeMake(60.f, 60.f);
}
}


@end
4 changes: 2 additions & 2 deletions NGTabBarController.xcodeproj/project.pbxproj
Expand Up @@ -84,12 +84,12 @@
9B81DCFA153C758C007FEE73 /* NGTabBarController.m */,
9B81DCFB153C758C007FEE73 /* NGTabBarControllerAnimation.h */,
9B81DCFC153C758C007FEE73 /* NGTabBarControllerDelegate.h */,
9B294EDA1546CB16004E4A63 /* NGTabBarItem.h */,
9B294EDB1546CB17004E4A63 /* NGTabBarItem.m */,
9B294ED41546BBDB004E4A63 /* NGTabBarPosition.h */,
9B81DCFD153C758C007FEE73 /* Prefix.pch */,
9B294ED61546C70B004E4A63 /* UIViewController+NGTabBarItem.h */,
9B294ED71546C70B004E4A63 /* UIViewController+NGTabBarItem.m */,
9B294EDA1546CB16004E4A63 /* NGTabBarItem.h */,
9B294EDB1546CB17004E4A63 /* NGTabBarItem.m */,
);
path = NGTabBarController;
sourceTree = "<group>";
Expand Down
12 changes: 7 additions & 5 deletions NGTabBarController/NGTabBar.m
@@ -1,6 +1,5 @@
#import "NGTabBar.h"
#import "NGTabBarItem.h"
#import <QuartzCore/QuartzCore.h>


@implementation NGTabBar
Expand All @@ -17,12 +16,10 @@ - (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.alwaysBounceHorizontal = NO;

_selectedItemIndex = 0;
_position = kNGTabBarPositionDefault;

self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 2.f;
}

return self;
Expand Down Expand Up @@ -64,7 +61,6 @@ - (void)setItems:(NSArray *)items {
_items = items;

for (NGTabBarItem *item in _items) {
item.frame = CGRectMake(0.f, 0.f, 44.f, 44.f);
[self addSubview:item];
}

Expand All @@ -76,6 +72,12 @@ - (void)setPosition:(NGTabBarPosition)position {
if (position != _position) {
_position = position;

if (NGTabBarIsVertical(position)) {
self.alwaysBounceVertical = YES;
} else {
self.alwaysBounceVertical = NO;
}

// TODO: re-compute contentSize

[self setNeedsLayout];
Expand Down
54 changes: 35 additions & 19 deletions NGTabBarController/NGTabBarController.m
Expand Up @@ -15,8 +15,6 @@ @interface NGTabBarController () {

// flags for methods implemented in the delegate
struct {
unsigned int widthOfTabBar:1;
unsigned int heightForTabBarCellAtIndex:1;
unsigned int shouldSelectViewController:1;
unsigned int didSelectViewController:1;
} _delegateFlags;
Expand All @@ -36,13 +34,15 @@ @interface NGTabBarController () {
- (void)updateUI;
- (void)layout;

- (CGFloat)delegatedTabBarWidth;
- (BOOL)delegatedDecisionIfWeShouldSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index;
- (void)callDelegateDidSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index;
- (CGSize)delegatedSizeOfItemForViewController:(UIViewController *)viewController atIndex:(NSUInteger)index position:(NGTabBarPosition)position;

- (void)setupTabBarForPosition:(NGTabBarPosition)position;
- (void)handleItemPressed:(id)sender;

- (CGFloat)widthOrHeightOfTabBarForPosition:(NGTabBarPosition)position;

@end


Expand Down Expand Up @@ -192,8 +192,6 @@ - (void)setDelegate:(id<NGTabBarControllerDelegate>)delegate {
_delegate = delegate;

// update delegate flags
_delegateFlags.widthOfTabBar = [delegate respondsToSelector:@selector(widthOfTabBarOfVerticalTabBarController:)];
_delegateFlags.heightForTabBarCellAtIndex = [delegate respondsToSelector:@selector(heightForTabBarCell:atIndex:)];
_delegateFlags.shouldSelectViewController = [delegate respondsToSelector:@selector(verticalTabBarController:shouldSelectViewController:atIndex:)];
_delegateFlags.didSelectViewController = [delegate respondsToSelector:@selector(verticalTabBarController:didSelectViewController:atIndex:)];
}
Expand Down Expand Up @@ -283,13 +281,13 @@ - (void)setTabBarPosition:(NGTabBarPosition)tabBarPosition {
- (void)setTabBarItems:(NSArray *)tabBarItems {
if (tabBarItems != _tabBarItems) {
for (NGTabBarItem *item in _tabBarItems) {
[item removeTarget:self action:@selector(handleItemPressed:) forControlEvents:UIControlEventTouchUpInside];
[item removeTarget:self action:@selector(handleItemPressed:) forControlEvents:UIControlEventTouchDown];
}

_tabBarItems = tabBarItems;

for (NGTabBarItem *item in _tabBarItems) {
[item addTarget:self action:@selector(handleItemPressed:) forControlEvents:UIControlEventTouchUpInside];
[item addTarget:self action:@selector(handleItemPressed:) forControlEvents:UIControlEventTouchDown];
}

self.tabBar.items = tabBarItems;
Expand Down Expand Up @@ -435,22 +433,23 @@ - (void)layout {
- (CGRect)childViewControllerFrame {
CGRect bounds = self.view.bounds;
UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
CGFloat inset = [self widthOrHeightOfTabBarForPosition:self.tabBarPosition];

switch (self.tabBarPosition) {
case NGTabBarPositionTop:
edgeInsets = UIEdgeInsetsMake([self delegatedTabBarWidth]+1.f, 0.f, 0.f, 0.f);
edgeInsets = UIEdgeInsetsMake(inset, 0.f, 0.f, 0.f);
break;

case NGTabBarPositionRight:
edgeInsets = UIEdgeInsetsMake(0.f, 0.f, 0.f, [self delegatedTabBarWidth]+1.f);
edgeInsets = UIEdgeInsetsMake(0.f, 0.f, 0.f, inset);
break;

case NGTabBarPositionBottom:
edgeInsets = UIEdgeInsetsMake(0.f, 0.f, [self delegatedTabBarWidth]+1.f, 0.f);
edgeInsets = UIEdgeInsetsMake(0.f, 0.f, inset, 0.f);
break;

case NGTabBarPositionLeft:
edgeInsets = UIEdgeInsetsMake(0.f, [self delegatedTabBarWidth]+1.f, 0.f, 0.f);
edgeInsets = UIEdgeInsetsMake(0.f, inset, 0.f, 0.f);
default:
break;
}
Expand Down Expand Up @@ -504,9 +503,7 @@ - (UIViewAnimationOptions)currentActiveAnimationOptions {
- (void)setupTabBarForPosition:(NGTabBarPosition)position {
CGRect frame = CGRectZero;
UIViewAutoresizing autoresizingMask = UIViewAutoresizingNone;

// TODO:
CGFloat dimension = [self delegatedTabBarWidth];
CGFloat dimension = [self widthOrHeightOfTabBarForPosition:position];

switch (position) {
case NGTabBarPositionTop: {
Expand Down Expand Up @@ -537,6 +534,13 @@ - (void)setupTabBarForPosition:(NGTabBarPosition)position {

self.tabBar.frame = frame;
self.tabBar.autoresizingMask = autoresizingMask;

for (NSUInteger index = 0; index < self.viewControllers.count; index++) {
UIViewController *viewController = [self.viewControllers objectAtIndex:index];
NGTabBarItem *item = [self.tabBarItems objectAtIndex:index];

[item setSize:[self delegatedSizeOfItemForViewController:viewController atIndex:index position:position]];
}
}

- (void)handleItemPressed:(id)sender {
Expand All @@ -548,13 +552,21 @@ - (void)handleItemPressed:(id)sender {
}
}

- (CGFloat)delegatedTabBarWidth {
if (_delegateFlags.widthOfTabBar) {
return [self.delegate widthOfTabBarOfTabBarController:self];
- (CGFloat)widthOrHeightOfTabBarForPosition:(NGTabBarPosition)position {
CGFloat dimension = kNGTabBarControllerDefaultWidth;

// first item is responsible for dimension of tabBar, all must be equal (will not be checked)
if (self.viewControllers.count > 0) {
CGSize size = [self delegatedSizeOfItemForViewController:[self.viewControllers objectAtIndex:0] atIndex:0 position:position];

if (NGTabBarIsVertical(position)) {
dimension = size.width;
} else {
dimension = size.height;
}
}

// default width
return kNGTabBarControllerDefaultWidth;
return dimension;
}

- (BOOL)delegatedDecisionIfWeShouldSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index {
Expand All @@ -572,4 +584,8 @@ - (void)callDelegateDidSelectViewController:(UIViewController *)viewController a
}
}

- (CGSize)delegatedSizeOfItemForViewController:(UIViewController *)viewController atIndex:(NSUInteger)index position:(NGTabBarPosition)position {
return [self.delegate tabBarController:self sizeOfItemForViewController:viewController atIndex:index position:position];
}

@end
13 changes: 10 additions & 3 deletions NGTabBarController/NGTabBarControllerDelegate.h
Expand Up @@ -6,15 +6,22 @@
// Copyright (c) 2012 NOUS Wissensmanagement GmbH. All rights reserved.
//

#import "NGTabBarPosition.h"

@class NGTabBarController;
@class NGTabBarItem;

@protocol NGTabBarControllerDelegate <NSObject>

@optional
@required

/** Asks the delegate for the width of the UITableView that acts as the tabBar, if the tabBar is displayed at the left/right */
- (CGFloat)widthOfTabBarOfTabBarController:(NGTabBarController *)tabBarController;
/** Asks the delegate for the size of the given item */
- (CGSize)tabBarController:(NGTabBarController *)tabBarController
sizeOfItemForViewController:(UIViewController *)viewController
atIndex:(NSUInteger)index
position:(NGTabBarPosition)position;

@optional

/** Asks the delegate whether the specified view controller should be made active. */
- (BOOL)tabBarController:(NGTabBarController *)tabBarController
Expand Down
2 changes: 2 additions & 0 deletions NGTabBarController/NGTabBarItem.h
Expand Up @@ -15,4 +15,6 @@

+ (NGTabBarItem *)itemWithTitle:(NSString *)title image:(UIImage *)image;

- (void)setSize:(CGSize)size;

@end
20 changes: 20 additions & 0 deletions NGTabBarController/NGTabBarItem.m
Expand Up @@ -54,6 +54,20 @@ - (void)layoutSubviews {
self.titleLabel.frame = self.bounds;
}

////////////////////////////////////////////////////////////////////////
#pragma mark - UIControl
////////////////////////////////////////////////////////////////////////

- (void)setSelected:(BOOL)selected {
[super setSelected:selected];

if (selected) {
self.backgroundColor = [UIColor greenColor];
} else {
self.backgroundColor = [UIColor redColor];
}
}

////////////////////////////////////////////////////////////////////////
#pragma mark - NGTabBarItem
////////////////////////////////////////////////////////////////////////
Expand All @@ -76,4 +90,10 @@ - (NSString *)title {
return self.titleLabel.text;
}

- (void)setSize:(CGSize)size {
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}

@end

0 comments on commit 19054e2

Please sign in to comment.