Permalink
Browse files

Possibility to center items in tabBar

  • Loading branch information...
1 parent f666c60 commit 113d5a6b3333867dbb6534c30f585bd00463e1ec @myell0w myell0w committed Apr 24, 2012
Showing with 37 additions and 0 deletions.
  1. +2 −0 NGTabBarController/NGTabBar.h
  2. +35 −0 NGTabBarController/NGTabBar.m
View
2 NGTabBarController/NGTabBar.h
@@ -15,6 +15,8 @@
@property (nonatomic, assign) NSUInteger selectedItemIndex;
@property (nonatomic, assign) NGTabBarPosition position;
+@property (nonatomic, assign) BOOL centerItems;
+
- (void)selectItemAtIndex:(NSUInteger)index;
- (void)deselectSelectedItem;
View
35 NGTabBarController/NGTabBar.m
@@ -2,11 +2,18 @@
#import "NGTabBarItem.h"
+@interface NGTabBar ()
+
+- (CGFloat)dimensionUsedOfItem:(NGTabBarItem *)item;
+
+@end
+
@implementation NGTabBar
@synthesize items = _items;
@synthesize selectedItemIndex = _selectedItemIndex;
@synthesize position = _position;
+@synthesize centerItems = _centerItems;
////////////////////////////////////////////////////////////////////////
#pragma mark - Lifecycle
@@ -19,6 +26,7 @@ - (id)initWithFrame:(CGRect)frame {
self.alwaysBounceHorizontal = NO;
_selectedItemIndex = 0;
+ _centerItems = NO;
_position = kNGTabBarPositionDefault;
}
@@ -35,6 +43,21 @@ - (void)layoutSubviews {
CGFloat currentFrameLeft = 0.f;
CGFloat currentFrameTop = 0.f;
+ if (self.centerItems) {
+ CGFloat totalDimension = 0.f;
+
+ // compute total dimension
+ for (NGTabBarItem *item in self.items) {
+ totalDimension += [self dimensionUsedOfItem:item];
+ }
+
+ if (NGTabBarIsVertical(self.position)) {
+ currentFrameTop = floorf((self.bounds.size.height-totalDimension)/2.f);
+ } else {
+ currentFrameLeft = floorf((self.bounds.size.width-totalDimension)/2.f);
+ }
+ }
+
for (NGTabBarItem *item in self.items) {
// re-position item
CGRect frame = item.frame;
@@ -104,4 +127,16 @@ - (void)deselectSelectedItem {
}
}
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Private
+////////////////////////////////////////////////////////////////////////
+
+- (CGFloat)dimensionUsedOfItem:(NGTabBarItem *)item {
+ if (NGTabBarIsVertical(self.position)) {
+ return item.frame.size.height;
+ } else {
+ return item.frame.size.width;
+ }
+}
+
@end

0 comments on commit 113d5a6

Please sign in to comment.