7
votes

I have the following view hierarchy:

Tab Bar Controller -> Navigation Controller -> Custom View Controller

In my Custom View I want the TabBar to disappear and show a toolbar instead. Much like in iOS7 native photos app when pressing 'select'.

I tried different solutions I found of SO but managed to get either:

  1. TabBar hidden and Toolbar shown with black gap
  2. TabBar hidden and Toolbar hidden
  3. TabBar hidden Toolbar shown with gap from bottom. However, Custom view content reaches the bottom of the screen (under the toolbar and in the same place the tab bar used to be)

The difference from other solutions I found is that I need this to happen on click and not on push.

Some of the things I tried:

// #1
[self.navigationController.toolbar setHidden:!isSelecting];
[self.tabBarController.tabBar setHidden:isSelecting];

// #2
self.hidesBottomBarWhenPushed = YES;

// #3
#1 & #2 variants @ different controller along the path
2
"The difference from other solutions I found is that I need this to happen on click and not on push." Umm... what?Lord Zsolt
Can you share some code or screenshots of what you achieved? Did you try resizing the view of your custom view controller to fill the black gap that you got?nburk
I suspect the Photos app is doing a modal presentation (with no animation) when you touch the "select" button. If you present a controller that has a toolbar, it would cover the tab bar, and give the look you see in Photos.rdelmar
@LordZsolt, I mean that I think that answers that suggested using hidesBottomBarWhenPushed won't work in this case. But maybe I'm wrong.Xyand
@NikolasBurk, I didn't try resizing. Added some code.Xyand

2 Answers

8
votes

Eventually, after playing with the settings I managed to make it work. I'm not sure why it works now and didn't work before so I'd appreciate your comments.

Storyboard:

  1. Mark as checked "Hide Bottom Bar on Push" for the Custom View Controller
  2. Mark as checked "Show Toolbar" for the Navigation Controller

Code:

On button click hide/unhide tabBar: [self.tabBarController.tabBar setHidden:state]

This almost works. It does hide/unhide the tabBar when pressing the button but the only problem is that the tabBar is initially hidden when switching tabs. I had to do some extra effort to have it visible.

Set UITabBarControllerDelegate to unhide tabBar when switching tabs. I did it in a custom SUSourceTabController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.delegate = self;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:   (UIViewController *)viewController
{
    [self.tabBar setHidden:NO];
}

We also need to unhide it for the first tab view in the Custom View Controller code. Using setHidden:NO in any other place in the code didn't work.

- (void)viewDidLoad
{
    [super viewDidLoad];    
    [self.tabBarController.tabBar setHidden:NO];
}
0
votes

Check this category from this question's answer.

UITabBarController+HideTabbar.h

#import <UIKit/UIKit.h>

@interface UITabBarController (HideTabbar)
- (void)setHidden:(BOOL)hidden animated:(BOOL)animated;
@end

UITabBarController+HideTabbar.m

#import "UITabBarController+HideTabbar.h"
#define kAnimationDuration .3
@implementation UITabBarController (HideTabbar)

- (void)setHidden:(BOOL)hidden animated:(BOOL)animated
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float fHeight = screenRect.size.height;
    if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        fHeight = screenRect.size.width;
    }
    if (!hidden) {
        fHeight -= self.tabBar.frame.size.height;
    }
    CGFloat animationDuration = animated ? kAnimationDuration : 0.f;
    [UIView animateWithDuration:animationDuration animations:^{
        for (UIView *view in self.view.subviews){
            if ([view isKindOfClass:[UITabBar class]]) {
                [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
            }
            else {
                if (hidden) {
                    [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
                }
            }
        }
    } completion:^(BOOL finished){
        if (!hidden){
            [UIView animateWithDuration:animationDuration animations:^{
                for(UIView *view in self.view.subviews) {
                    if (![view isKindOfClass:[UITabBar class]]) {
                        [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
                    }
                }
            }];
        }
    }];
}

@end