7

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
Xyand
  • 4,470
  • 4
  • 36
  • 63
  • 1
    "The difference from other solutions I found is that I need this to happen on click and not on push." Umm... what? – Lord Zsolt Jun 22 '14 at 16:30
  • 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 Jun 22 '14 at 16:30
  • 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 Jun 22 '14 at 16:54
  • @LordZsolt, I mean that I think that answers that suggested using `hidesBottomBarWhenPushed` won't work in this case. But maybe I'm wrong. – Xyand Jun 22 '14 at 16:59
  • @NikolasBurk, I didn't try resizing. Added some code. – Xyand Jun 22 '14 at 17:00
  • @rdelmar, But it seems that the 'new' view preserves the same state as before pressing select. Is this doable with a modal? – Xyand Jun 22 '14 at 17:01

2 Answers2

8

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];
}
Xyand
  • 4,470
  • 4
  • 36
  • 63
  • 1
    Ah, it really helped me. But, for me, setting "Hide Bottom Bar on Push" to true was just enough. Thanks! – Next Developer Mar 20 '16 at 17:46
  • 1
    The only answer that finally got me what I wanted. Need to have a table VC controller in one tab have an "Edit" option. Edit mode allows multiple selection. I need to hide the tab bar and show a toolbar. I'm using iOS 9 and toggling the tabBar and toolbar states was easy enough (I did the same was as OP). This answer showed how to prevent the tabBar from hiding when switching tabs. Also, I didn't need to do the second part ("unhide it for the first tab view") because I was using the second tab as the starting tab (using selectedIndex = 1 in the viewDidLoad of custom TabController) – Protongun Mar 24 '16 at 23:26
  • Can not remove the gap (xcode 8, iOS 8.2). To fix the issue, have to use another toolbar instead of self.navigationController.toolbar, and add a constraint 'Bottom Space to Superview = 0' instead of to Bottom Layout Guide. – Nianliang Oct 23 '16 at 13:51
  • It doesn't look as expected on iPhone X if you constraint bottom to superview instead of layout guide https://stackoverflow.com/questions/48236375/custom-uitoolbar-too-close-to-the-home-indicator-on-iphone-x – nambatee Jan 14 '18 at 04:17
  • Implemented @Xyand's solution and works properly on iPhone X. – Shai Ben-Tovim Feb 20 '18 at 11:47
  • Are you not facing any issues on iOS13? I'm facing animation issues while transitioning from tab-bar to toolbar – harshith__ Sep 10 '19 at 07:15
0

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
Community
  • 1
  • 1
Mustafa Ahmed
  • 422
  • 2
  • 8