Permalink
Browse files

Different layoutStrategies for tabBar, lazy loading tabBar

  • Loading branch information...
myell0w committed Apr 25, 2012
1 parent 91a129b commit cb8d7ede4d553866c4efe7a79495a66410171483
Showing with 62 additions and 17 deletions.
  1. +10 −2 NGTabBarController/NGTabBar.h
  2. +42 −14 NGTabBarController/NGTabBar.m
  3. +10 −1 NGTabBarController/NGTabBarController.m
@@ -9,13 +9,21 @@
#import <UIKit/UIKit.h>
#import "NGTabBarPosition.h"
+typedef enum {
+ NGTabBarLayoutStrategyStrungTogether = 0,
+ NGTabBarLayoutStrategyEvenlyDistributed,
+ NGTabBarLayoutStrategyCentered
+} NGTabBarLayoutStrategy;
+
+
@interface NGTabBar : UIScrollView
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, assign) NSUInteger selectedItemIndex;
@property (nonatomic, assign) NGTabBarPosition position;
-
-@property (nonatomic, assign) BOOL centerItems;
+@property (nonatomic, assign) NGTabBarLayoutStrategy layoutStrategy;
+/** the padding to apply between items, not taken into account when layoutStrategy is EvenlyDistributed */
+@property (nonatomic, assign) CGFloat itemPadding;
- (void)selectItemAtIndex:(NSUInteger)index;
- (void)deselectSelectedItem;
@@ -4,7 +4,7 @@
@interface NGTabBar ()
-- (CGFloat)dimensionUsedOfItem:(NGTabBarItem *)item;
+- (CGFloat)dimensionToBeConsideredOfItem:(NGTabBarItem *)item;
@end
@@ -13,7 +13,8 @@ @implementation NGTabBar
@synthesize items = _items;
@synthesize selectedItemIndex = _selectedItemIndex;
@synthesize position = _position;
-@synthesize centerItems = _centerItems;
+@synthesize layoutStrategy = _layoutStrategy;
+@synthesize itemPadding = _itemPadding;
////////////////////////////////////////////////////////////////////////
#pragma mark - Lifecycle
@@ -27,7 +28,8 @@ - (id)initWithFrame:(CGRect)frame {
self.clipsToBounds = YES;
_selectedItemIndex = 0;
- _centerItems = NO;
+ _layoutStrategy = NGTabBarLayoutStrategyStrungTogether;
+ _itemPadding = 0.f;
_position = kNGTabBarPositionDefault;
}
@@ -43,33 +45,59 @@ - (void)layoutSubviews {
CGFloat currentFrameLeft = 0.f;
CGFloat currentFrameTop = 0.f;
+ CGFloat totalDimension = 0.f;
+ // we change item padding in strategy evenly distributed but don't want to change iVar
+ CGFloat appliedItemPadding = self.itemPadding;
- if (self.centerItems) {
- CGFloat totalDimension = 0.f;
-
- // compute total dimension
+ if (self.layoutStrategy == NGTabBarLayoutStrategyEvenlyDistributed || self.layoutStrategy == NGTabBarLayoutStrategyCentered) {
+ // compute total dimension needed
for (NGTabBarItem *item in self.items) {
- totalDimension += [self dimensionUsedOfItem:item];
+ totalDimension += [self dimensionToBeConsideredOfItem:item];
+
+ // we don't take padding only into account if we want to evenly distribute items
+ if (self.layoutStrategy != NGTabBarLayoutStrategyEvenlyDistributed) {
+ totalDimension += self.itemPadding;
+ }
}
- if (NGTabBarIsVertical(self.position)) {
- currentFrameTop = floorf((self.bounds.size.height-totalDimension)/2.f);
- } else {
- currentFrameLeft = floorf((self.bounds.size.width-totalDimension)/2.f);
+ // for evenly distributed items we calculate a new item padding
+ if (self.layoutStrategy == NGTabBarLayoutStrategyEvenlyDistributed) {
+ // the total padding needed for the whole tabBar
+ CGFloat totalPadding = NGTabBarIsVertical(self.position) ? self.bounds.size.height - totalDimension : self.bounds.size.width - totalDimension;
+
+ // we apply the padding (items.count - 1) times (always between two items)
+ if (self.items.count > 1) {
+ appliedItemPadding = MAX(0.f,totalPadding / (self.items.count - 1));
+ }
+ }
+
+ else if (self.layoutStrategy == NGTabBarLayoutStrategyCentered) {
+ // we only add padding between icons but we added it for each item in the loop above
+ totalDimension -= appliedItemPadding;
+
+ if (NGTabBarIsVertical(self.position)) {
+ currentFrameTop = floorf((self.bounds.size.height-totalDimension)/2.f);
+ } else {
+ currentFrameLeft = floorf((self.bounds.size.width-totalDimension)/2.f);
+ }
}
}
+ // re-position each item starting from current top/left
for (NGTabBarItem *item in self.items) {
- // re-position item
CGRect frame = item.frame;
+
frame.origin.y = currentFrameTop;
frame.origin.x = currentFrameLeft;
item.frame = frame;
+ // move to next item position
if (NGTabBarIsVertical(self.position)) {
currentFrameTop += frame.size.height;
+ currentFrameTop += appliedItemPadding;
} else {
currentFrameLeft += frame.size.width;
+ currentFrameLeft += appliedItemPadding;
}
}
}
@@ -132,7 +160,7 @@ - (void)deselectSelectedItem {
#pragma mark - Private
////////////////////////////////////////////////////////////////////////
-- (CGFloat)dimensionUsedOfItem:(NGTabBarItem *)item {
+- (CGFloat)dimensionToBeConsideredOfItem:(NGTabBarItem *)item {
if (NGTabBarIsVertical(self.position)) {
return item.frame.size.height;
} else {
@@ -91,7 +91,6 @@ - (void)viewDidLoad {
NSAssert(self.delegate != nil, @"No delegate set");
- self.tabBar = [[NGTabBar alloc] initWithFrame:CGRectZero];
self.tabBar.items = self.tabBarItems;
self.tabBar.position = self.tabBarPosition;
@@ -187,6 +186,14 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO
#pragma mark - NGTabBarController
////////////////////////////////////////////////////////////////////////
+- (NGTabBar *)tabBar {
+ if (_tabBar == nil) {
+ _tabBar = [[NGTabBar alloc] initWithFrame:CGRectZero];
+ }
+
+ return _tabBar;
+}
+
- (void)setDelegate:(id<NGTabBarControllerDelegate>)delegate {
if (delegate != _delegate) {
_delegate = delegate;
@@ -416,6 +423,8 @@ - (void)updateUI {
[self.view addSubview:newSelectedViewController.view];
}
}
+ } else {
+ [self.tabBar deselectSelectedItem];
}
}

0 comments on commit cb8d7ed

Please sign in to comment.