Skip to content

Instantly share code, notes, and snippets.

@JRG-Developer
Last active March 2, 2019 18:48
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save JRG-Developer/6714930 to your computer and use it in GitHub Desktop.
Save JRG-Developer/6714930 to your computer and use it in GitHub Desktop.
iOS 6+ Method for Hiding the Tab Bar
//
// UITabBarController+HideTabBar.h
//
// Created by Joshua Greene on 9/26/13.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
@interface UITabBarController (Additions)
- (void)setTabBarHidden:(BOOL)hidden;
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;
@end
//
// UITabBarController+HideTabBar.m
//
// Created by Joshua Greene on 9/26/13.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "UITabBarController+HideTabBar.h"
@implementation UITabBarController (HideTabBar)
- (void)setTabBarHidden:(BOOL)hidden
{
[self setTabBarHidden:hidden animated:YES];
}
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated
{
if (self.tabBar.hidden == hidden)
return;
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
BOOL isLandscape = UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
float height = isLandscape ? screenSize.width : screenSize.height;
if (!hidden)
height -= CGRectGetHeight(self.tabBar.frame);
void (^animations)();
// Check if running iOS 7
if ([self.tabBar respondsToSelector:@selector(barTintColor)])
{
UIView *view = self.selectedViewController.view;
animations = ^{
CGRect frame = self.tabBar.frame;
frame.origin.y = height;
self.tabBar.frame = frame;
frame = view.frame;
frame.size.height += (hidden ? 1.0f : -1.0f) * CGRectGetHeight(self.tabBar.frame);
view.frame = frame;
};
}
else
{
animations = ^{
for (UIView *subview in self.view.subviews)
{
CGRect frame = subview.frame;
if (subview == self.tabBar)
{
frame.origin.y = height;
}
else
{
frame.size.height = height;
}
subview.frame = frame;
}
};
}
[UIView animateWithDuration:(animated ? 0.25f : 0) animations:animations completion:^(BOOL finished) {
self.tabBar.hidden = hidden;
}];
}
@end
@camdenfullmer
Copy link

I found a problem with this code where if I hide the tab bar and then send the app to the background, upon reopening it one of the layout containers gets resized to fit a blank tab bar at the bottom of the screen. Then when I show the tab bar again the tab bar slides up but there is a box above it with enough room for another tab bar.

@dusiema
Copy link

dusiema commented May 13, 2014

I do have the exact same problem. There is a black box with the same size of the tabbar.

@dusiema
Copy link

dusiema commented May 14, 2014

One workaround for this (which a friend found out) is you can set the attribute extendedLayoutIncludesOpaqueBars on the view controller to YES. Either programmatically (e.g. in viewDidLoad) or in interface builder.

@renfeisong
Copy link

Change

if (!hidden)
    height -= CGRectGetHeight(self.tabBar.frame);

To

if (!hidden) {
    height -= CGRectGetHeight(self.tabBar.frame);
    self.tabBar.hidden = NO;
}

You must set self.tabBar's hidden property to NO prior to any animation (therefore left only frame to be animated). Otherwise nothing will be visible during the animation because the tab bar is hidden all the time.

@fitch
Copy link

fitch commented Jan 26, 2016

.h is missing @import UIKit;.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment