Skip to content

Instantly share code, notes, and snippets.

@CavalcanteLeo
Last active July 24, 2017 18:46
Show Gist options
  • Save CavalcanteLeo/d1745fde0348c31c50629ec8e9d01a98 to your computer and use it in GitHub Desktop.
Save CavalcanteLeo/d1745fde0348c31c50629ec8e9d01a98 to your computer and use it in GitHub Desktop.
//
// BaseTabBarController.h
// WeCareWealthManagement
//
// Created by Leo Cavalcante on 24/07/17.
// Copyright © 2017 Kiddo. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface BaseTabBarController : UITabBarController
@property (nonatomic, getter=isTabBarHidden,readonly)BOOL tabBarHidden;
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;
@end
//
// BaseTabBarController.m
// WeCareWealthManagement
//
// Created by Leo Cavalcante on 24/07/17.
// Copyright © 2017 Kiddo. All rights reserved.
//
#import "BaseTabBarController.h"
@interface BaseTabBarController () <UITabBarControllerDelegate>
@end
@implementation BaseTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.delegate = self;
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
self.tabBar.barTintColor = [UIColor whiteColor];
self.tabBar.tintColor = [UIColor blackColor];
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tabBar.frame.size.width, 1)];
line.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.tabBar addSubview:line];
}
#pragma mark - Animation on ViewController switch
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger controllerIndex = [self.viewControllers indexOfObject:viewController];
if (viewController == [tabBarController selectedViewController]) {
if ([viewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController*)viewController popToRootViewControllerAnimated:YES];
}
return NO;
}
// Get the views.
UIView *fromView = tabBarController.selectedViewController.view;
UIView *toView = [tabBarController.viewControllers[controllerIndex] view];
[fromView.superview addSubview:toView];
UIViewController *fromViewVC = [tabBarController.selectedViewController childViewControllers][0];
UIViewController *toViewVC = [viewController childViewControllers][0];
BOOL scrollRight = controllerIndex > tabBarController.selectedIndex;
// Position it off screen.
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width / 2 ;
[toViewVC.view setTransform:CGAffineTransformMakeTranslation((scrollRight ? screenWidth : -screenWidth), 0)];
fromViewVC.view.center = CGPointMake(fromViewVC.view.center.x, fromViewVC.view.center.y);
toViewVC.view.alpha = 0.1;
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
fromViewVC.view.center = CGPointMake(fromViewVC.view.center.x - (scrollRight ? screenWidth-100 : -screenWidth+100), fromViewVC.view.center.y);
[toViewVC.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
toViewVC.view.alpha = 1;
}
completion:^(BOOL finished) {
if (finished) {
[toViewVC.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
[fromViewVC.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
// Remove the old view from the tabbar view.
[fromView removeFromSuperview];
tabBarController.selectedIndex = controllerIndex;
}
}];
return NO;
}
#pragma mark - Hide TabBar
- (BOOL)isTabBarHidden{
CGRect viewFrame = self.view.frame;
CGRect tabBarFrame = self.tabBar.frame;
return tabBarFrame.origin.y >= viewFrame.size.height;
}
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
BOOL isHidden = self.tabBarHidden;
if (hidden == isHidden) {
return;
}
UIView *transitionView = [[[self.view.subviews reverseObjectEnumerator] allObjects] lastObject];
if(transitionView == nil) {
NSLog(@"could not get the container view!");
return;
}
CGRect viewFrame = self.view.frame;
CGRect tabBarFrame = self.tabBar.frame;
CGRect containerFrame = transitionView.frame;
tabBarFrame.origin.y = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height);
containerFrame.size.height = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height);
if (animated) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35];
}
self.tabBar.frame = tabBarFrame;
transitionView.frame = containerFrame;
if (animated) {
[UIView commitAnimations];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment