Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Delegated size of tabBar-item

  • Loading branch information...
commit 19054e220871c90ed8f1cc2b2a2ae2f8888c8301 1 parent 35f8ed2
@myell0w myell0w authored
View
15 Demo/NGVerticalTabBarControllerDemo/NGVerticalTabBarControllerDemo/AppDelegate.m
@@ -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
View
4 NGTabBarController.xcodeproj/project.pbxproj
@@ -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>";
View
12 NGTabBarController/NGTabBar.m
@@ -1,6 +1,5 @@
#import "NGTabBar.h"
#import "NGTabBarItem.h"
-#import <QuartzCore/QuartzCore.h>
@implementation NGTabBar
@@ -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;
@@ -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];
}
@@ -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];
View
54 NGTabBarController/NGTabBarController.m
@@ -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;
@@ -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
@@ -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:)];
}
@@ -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;
@@ -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;
}
@@ -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: {
@@ -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 {
@@ -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 {
@@ -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
View
13 NGTabBarController/NGTabBarControllerDelegate.h
@@ -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
View
2  NGTabBarController/NGTabBarItem.h
@@ -15,4 +15,6 @@
+ (NGTabBarItem *)itemWithTitle:(NSString *)title image:(UIImage *)image;
+- (void)setSize:(CGSize)size;
+
@end
View
20 NGTabBarController/NGTabBarItem.m
@@ -55,6 +55,20 @@ - (void)layoutSubviews {
}
////////////////////////////////////////////////////////////////////////
+#pragma mark - UIControl
+////////////////////////////////////////////////////////////////////////
+
+- (void)setSelected:(BOOL)selected {
+ [super setSelected:selected];
+
+ if (selected) {
+ self.backgroundColor = [UIColor greenColor];
+ } else {
+ self.backgroundColor = [UIColor redColor];
+ }
+}
+
+////////////////////////////////////////////////////////////////////////
#pragma mark - NGTabBarItem
////////////////////////////////////////////////////////////////////////
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.