Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed some problems about hidesBottomBarWhenPushed=YES controller #13

Open
wants to merge 5 commits into from

1 participant

@luosky

After pushing two hidesBottomBarWhenPushed=YES controller one by one, then pop a controller up, a fake tabbar will remain on the first hidesBottomBarWhenPushed=YES controller.

And if I started with a hidesBottomBarWhenPushed=NO controller , then I push two hidesBottomBarWhenPushed=YES controllers , then pop to rootViewController, the rootViewController will have two tabbars. One is a real tab bar, the other is a fake tab bar image.

This pull request fixed these two problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 24, 2012
  1. @luosky

    fixed a bug when pushing more than one hidesBottomBarWhenPushed contr…

    luosky authored
    …oller , only the last controller's tabBar will be hide. Others end up with a fake tabBar.
  2. @luosky
Commits on Oct 31, 2012
  1. @luosky
  2. @luosky

    compatible with storyboard

    luosky authored
Commits on Nov 15, 2012
  1. @luosky
This page is out of date. Refresh to see the latest.
View
2  NGTabBarController/NGTabBarController.h
@@ -47,5 +47,7 @@
- (id)initWithDelegate:(id<NGTabBarControllerDelegate>)delegate;
- (void)setTabBarHidden:(BOOL)tabBarHidden animated:(BOOL)animated;
+- (void)selectTabAtIndex:(NSInteger)index;
+- (void)configTabBar;
@end
View
61 NGTabBarController/NGTabBarController.m
@@ -67,15 +67,18 @@ @implementation NGTabBarController
////////////////////////////////////////////////////////////////////////
#pragma mark - Lifecycle
////////////////////////////////////////////////////////////////////////
+- (void)configTabBar{
+ _selectedIndex = NSNotFound;
+ _oldSelectedIndex = NSNotFound;
+ _animation = NGTabBarControllerAnimationNone;
+ _animationDuration = kNGDefaultAnimationDuration;
+ _animationActive = NO;
+ _tabBarPosition = kNGTabBarPositionDefault;
+}
- (id)initWithDelegate:(id<NGTabBarControllerDelegate>)delegate {
if ((self = [super initWithNibName:nil bundle:nil])) {
- _selectedIndex = NSNotFound;
- _oldSelectedIndex = NSNotFound;
- _animation = NGTabBarControllerAnimationNone;
- _animationDuration = kNGDefaultAnimationDuration;
- _animationActive = NO;
- _tabBarPosition = kNGTabBarPositionDefault;
+ [self configTabBar];
// need to call setter here
self.delegate = delegate;
@@ -88,6 +91,13 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
return [self init];
}
+- (id)initWithCoder:(NSCoder *)aDecoder{
+ if ((self = [super initWithNibName:nil bundle:nil])) {
+ [self configTabBar];
+ }
+ return self;
+}
+
////////////////////////////////////////////////////////////////////////
#pragma mark - UIViewController
////////////////////////////////////////////////////////////////////////
@@ -368,22 +378,24 @@ - (void)navigationController:(UINavigationController *)navigationController will
}
if (viewController.hidesBottomBarWhenPushed) {
- NSUInteger indexOfViewControllerToPush = [navigationController.viewControllers indexOfObject:viewController];
- NSInteger indexOfViewControllerThatGetsHidden = indexOfViewControllerToPush - 1;
+ [self setTabBarHidden:YES animated:YES];
+ }
+
+ // add image of tabBar to the viewController's view to get a nice animation when hidesBottomBarWhenPushed property changed
+ UIViewController *currentViewController = navigationController.topViewController;
+ NSUInteger indexOfNextViewController = [navigationController.viewControllers indexOfObject:viewController];
+ NSUInteger indexOfCurrentViewController = [navigationController.viewControllers indexOfObject:currentViewController];
+ BOOL isPush = indexOfNextViewController > indexOfCurrentViewController; //don't add image when popping in navigationController
+ if (isPush && viewController.hidesBottomBarWhenPushed && !currentViewController.hidesBottomBarWhenPushed ) {
+ UIImageView *tabBarImageRepresentation = [self.tabBar imageViewRepresentation];
- 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];
- }
+ tabBarImageRepresentation.frame = CGRectMake(0.f,currentViewController.view.frame.origin.y + currentViewController.view.frame.size.height - tabBarImageRepresentation.frame.size.height,
+ tabBarImageRepresentation.frame.size.width,tabBarImageRepresentation.frame.size.height);
+
+ objc_setAssociatedObject(currentViewController, &tabBarImageViewKey, tabBarImageRepresentation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ [currentViewController.view addSubview:tabBarImageRepresentation];
}
+
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
@@ -394,15 +406,14 @@ - (void)navigationController:(UINavigationController *)navigationController didS
}
if (!viewController.hidesBottomBarWhenPushed) {
- [self setTabBarHidden:NO animated:NO];
-
+ [self setTabBarHidden:NO animated:YES];
+
// Remove temporary tabBar image
UIView *view = objc_getAssociatedObject(viewController, &tabBarImageViewKey);
[view removeFromSuperview];
objc_setAssociatedObject(viewController, &tabBarImageViewKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
-
////////////////////////////////////////////////////////////////////////
#pragma mark - Private
////////////////////////////////////////////////////////////////////////
@@ -659,6 +670,10 @@ - (void)setupTabBarForPosition:(NGTabBarPosition)position {
- (void)handleItemPressed:(id)sender {
NSInteger index = [self.tabBarItems indexOfObject:sender];
+ [self selectTabAtIndex:index];
+}
+
+- (void)selectTabAtIndex:(NSInteger)index{
BOOL shouldSelect = [self delegatedDecisionIfWeShouldSelectViewController:[self.viewControllers objectAtIndex:index] atIndex:index];
if (shouldSelect) {
Something went wrong with that request. Please try again.