Skip to content
Browse files

First version of hidesBottomBarWhenPushed, won't work very well with …

…rotation I guess ;)
  • Loading branch information...
1 parent c8b1135 commit 176e730a48a06402d56fb9db779097393acd2d6d @myell0w myell0w committed May 21, 2012
View
8 NGTabBarController.xcodeproj/project.pbxproj
@@ -11,6 +11,8 @@
9B294ED91546C70B004E4A63 /* UIViewController+NGTabBarItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B294ED71546C70B004E4A63 /* UIViewController+NGTabBarItem.m */; };
9B294EDC1546CB17004E4A63 /* NGTabBarItem.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B294EDA1546CB16004E4A63 /* NGTabBarItem.h */; };
9B294EDD1546CB17004E4A63 /* NGTabBarItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B294EDB1546CB17004E4A63 /* NGTabBarItem.m */; };
+ 9B38245E156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B38245C156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.h */; };
+ 9B38245F156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B38245D156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.m */; };
9B80B53114EA8F7100B83FA0 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B80B53014EA8F7100B83FA0 /* Foundation.framework */; };
9B81DCFE153C758C007FEE73 /* NGTabBar.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B81DCF5153C758C007FEE73 /* NGTabBar.h */; };
9B81DCFF153C758C007FEE73 /* NGTabBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B81DCF6153C758C007FEE73 /* NGTabBar.m */; };
@@ -27,6 +29,8 @@
9B294ED71546C70B004E4A63 /* UIViewController+NGTabBarItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+NGTabBarItem.m"; sourceTree = "<group>"; };
9B294EDA1546CB16004E4A63 /* NGTabBarItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NGTabBarItem.h; sourceTree = "<group>"; };
9B294EDB1546CB17004E4A63 /* NGTabBarItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NGTabBarItem.m; sourceTree = "<group>"; };
+ 9B38245C156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UINavigationController+NGTabBarNavigationDelegate.h"; sourceTree = "<group>"; };
+ 9B38245D156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UINavigationController+NGTabBarNavigationDelegate.m"; sourceTree = "<group>"; };
9B80B52D14EA8F7100B83FA0 /* libNGTabBarController.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libNGTabBarController.a; sourceTree = BUILT_PRODUCTS_DIR; };
9B80B53014EA8F7100B83FA0 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
9B81DCF5153C758C007FEE73 /* NGTabBar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NGTabBar.h; sourceTree = "<group>"; };
@@ -90,6 +94,8 @@
9B81DCFD153C758C007FEE73 /* Prefix.pch */,
9B294ED61546C70B004E4A63 /* UIViewController+NGTabBarItem.h */,
9B294ED71546C70B004E4A63 /* UIViewController+NGTabBarItem.m */,
+ 9B38245C156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.h */,
+ 9B38245D156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.m */,
);
path = NGTabBarController;
sourceTree = "<group>";
@@ -108,6 +114,7 @@
9B81DD06153C758C007FEE73 /* Prefix.pch in Headers */,
9B294ED81546C70B004E4A63 /* UIViewController+NGTabBarItem.h in Headers */,
9B294EDC1546CB17004E4A63 /* NGTabBarItem.h in Headers */,
+ 9B38245E156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -166,6 +173,7 @@
9B81DD03153C758C007FEE73 /* NGTabBarController.m in Sources */,
9B294ED91546C70B004E4A63 /* UIViewController+NGTabBarItem.m in Sources */,
9B294EDD1546CB17004E4A63 /* NGTabBarItem.m in Sources */,
+ 9B38245F156A82A400C8D1F4 /* UINavigationController+NGTabBarNavigationDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
2 NGTabBarController/NGTabBar.h
@@ -37,4 +37,6 @@ typedef enum {
- (void)selectItemAtIndex:(NSUInteger)index;
- (void)deselectSelectedItem;
+- (UIImageView *)imageViewRepresentation;
+
@end
View
14 NGTabBarController/NGTabBar.m
@@ -291,6 +291,20 @@ - (void)setShowsItemHighlight:(BOOL)showsItemHighlight {
}
}
+- (UIImageView *)imageViewRepresentation {
+ UIGraphicsBeginImageContext(self.bounds.size);
+ [self.layer renderInContext:UIGraphicsGetCurrentContext()];
+ UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+
+ UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
+ imageView.backgroundColor = [UIColor redColor];
+ imageView.frame = self.frame;
+ imageView.autoresizingMask = self.autoresizingMask;
+
+ return imageView;
+}
+
////////////////////////////////////////////////////////////////////////
#pragma mark - Private
////////////////////////////////////////////////////////////////////////
View
2 NGTabBarController/NGTabBarController.h
@@ -18,7 +18,7 @@
/** NGTabBarController is a customized TabBar displayed on any side of the device */
-@interface NGTabBarController : UIViewController
+@interface NGTabBarController : UIViewController <UINavigationControllerDelegate>
/** An array of the view controllers displayed by the tab bar */
@property (nonatomic, copy) NSArray *viewControllers;
View
70 NGTabBarController/NGTabBarController.m
@@ -1,4 +1,5 @@
#import "NGTabBarController.h"
+#import "UINavigationController+NGTabBarNavigationDelegate.h"
#import <objc/runtime.h>
@@ -10,6 +11,9 @@
#define kNGScaleDuration 0.15f
+static char tabBarImageViewKey;
+
+
@interface NGTabBarController () {
// re-defined as mutable
NSMutableArray *_viewControllers;
@@ -233,7 +237,15 @@ - (void)setViewControllers:(NSArray *)viewControllers {
// remove old child view controller
for (UIViewController *viewController in _viewControllers) {
[viewController removeFromParentViewController];
+
objc_setAssociatedObject(viewController, kNGTabBarControllerKey, nil, OBJC_ASSOCIATION_ASSIGN);
+
+ // reset navigationControllerDelegate
+ if ([viewController isKindOfClass:[UINavigationController class]]) {
+ UINavigationController *navigationController = (UINavigationController *)viewController;
+
+ navigationController.delegate = navigationController.ng_originalNavigationControllerDelegate;
+ }
}
}
@@ -253,7 +265,18 @@ - (void)setViewControllers:(NSArray *)viewControllers {
viewController.view.frame = childViewControllerFrame;
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
viewController.view.clipsToBounds = YES;
+ // store read-only reference to tabBarController
objc_setAssociatedObject(viewController, kNGTabBarControllerKey, self, OBJC_ASSOCIATION_ASSIGN);
+
+ // set ourselve as navigationControllerDelegate and store the previous one
+ // that's because we need to know when a VC gets pushed and check if it has
+ // hidesBottomBarWhenPushed set
+ if ([viewController isKindOfClass:[UINavigationController class]]) {
+ UINavigationController *navigationController = (UINavigationController *)viewController;
+
+ navigationController.ng_originalNavigationControllerDelegate = navigationController.delegate;
+ navigationController.delegate = self;
+ }
}
[self layout];
@@ -335,6 +358,53 @@ - (NSTimeInterval)animationDuration {
}
////////////////////////////////////////////////////////////////////////
+#pragma mark - UINavigationControllerDelegate
+////////////////////////////////////////////////////////////////////////
+
+- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
+ // don't call the delegate if the original tabBarController is a subclass of
+ // NGTabBarController to prevent a infinite loop
+ if (![navigationController.ng_originalNavigationControllerDelegate isKindOfClass:[self class]]) {
+ [navigationController.ng_originalNavigationControllerDelegate navigationController:navigationController willShowViewController:viewController animated:animated];
+ }
+
+ if (viewController.hidesBottomBarWhenPushed) {
+ NSUInteger indexOfViewControllerToPush = [navigationController.viewControllers indexOfObject:viewController];
+ NSInteger indexOfViewControllerThatGetsHidden = indexOfViewControllerToPush - 1;
+
+ if (indexOfViewControllerThatGetsHidden >= 0) {
+ // add image of tabBar to the viewController's view to get a nice animation
+ UIViewController *viewControllerThatGetsHidden = [navigationController.viewControllers objectAtIndex:indexOfViewControllerThatGetsHidden];
+ UIImageView *tabBarImageRepresentation = [self.tabBar imageViewRepresentation];
+
+ tabBarImageRepresentation.frame = CGRectMake(0.f,viewControllerThatGetsHidden.view.frame.origin.y + viewControllerThatGetsHidden.view.frame.size.height - tabBarImageRepresentation.frame.size.height,
+ tabBarImageRepresentation.frame.size.width,tabBarImageRepresentation.frame.size.height);
+
+ objc_setAssociatedObject(viewControllerThatGetsHidden, &tabBarImageViewKey, tabBarImageRepresentation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ [viewControllerThatGetsHidden.view addSubview:tabBarImageRepresentation];
+ [self setTabBarHidden:YES animated:NO];
+ }
+ }
+}
+
+- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
+ // don't call the delegate if the original tabBarController is a subclass of
+ // NGTabBarController to prevent a infinite loop
+ if (![navigationController.ng_originalNavigationControllerDelegate isKindOfClass:[self class]]) {
+ [navigationController.ng_originalNavigationControllerDelegate navigationController:navigationController willShowViewController:viewController animated:animated];
+ }
+
+ if (!viewController.hidesBottomBarWhenPushed) {
+ [self setTabBarHidden:NO animated:NO];
+
+ // Remove temporary tabBar image
+ UIView *view = objc_getAssociatedObject(viewController, &tabBarImageViewKey);
+ [view removeFromSuperview];
+ objc_setAssociatedObject(viewController, &tabBarImageViewKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ }
+}
+
+////////////////////////////////////////////////////////////////////////
#pragma mark - Private
////////////////////////////////////////////////////////////////////////
View
15 NGTabBarController/UINavigationController+NGTabBarNavigationDelegate.h
@@ -0,0 +1,15 @@
+//
+// UINavigationController+NGTabBarNavigationDelegate.h
+// NGTabBarController
+//
+// Created by Tretter Matthias on 21.05.12.
+// Copyright (c) 2012 NOUS Wissensmanagement GmbH. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface UINavigationController (NGTabBarNavigationDelegate)
+
+@property (nonatomic, assign, setter = ng_setOriginalNavigationControllerDelegate:) id<UINavigationControllerDelegate> ng_originalNavigationControllerDelegate;
+
+@end
View
26 NGTabBarController/UINavigationController+NGTabBarNavigationDelegate.m
@@ -0,0 +1,26 @@
+//
+// UINavigationController+NGTabBarNavigationDelegate.m
+// NGTabBarController
+//
+// Created by Tretter Matthias on 21.05.12.
+// Copyright (c) 2012 NOUS Wissensmanagement GmbH. All rights reserved.
+//
+
+#import "UINavigationController+NGTabBarNavigationDelegate.h"
+#import <objc/runtime.h>
+
+
+static char originalDelegateKey;
+
+
+@implementation UINavigationController (NGTabBarNavigationDelegate)
+
+- (void)ng_setOriginalNavigationControllerDelegate:(id<UINavigationControllerDelegate>)ng_originalNavigationControllerDelegate {
+ objc_setAssociatedObject(self, &originalDelegateKey, ng_originalNavigationControllerDelegate, OBJC_ASSOCIATION_ASSIGN);
+}
+
+- (id<UINavigationControllerDelegate>)ng_originalNavigationControllerDelegate {
+ return objc_getAssociatedObject(self, &originalDelegateKey);
+}
+
+@end

0 comments on commit 176e730

Please sign in to comment.
Something went wrong with that request. Please try again.