Skip to content

Instantly share code, notes, and snippets.

@nishitpatel
Created January 12, 2017 11:54
Show Gist options
  • Save nishitpatel/c9a44bc3804690c164c30a84cdabb6a1 to your computer and use it in GitHub Desktop.
Save nishitpatel/c9a44bc3804690c164c30a84cdabb6a1 to your computer and use it in GitHub Desktop.
UITabBarController Customization 1. UITabBar Shift to Top of the Screen, 2.Add Custome Bottom Border, 3. Add Left to Right swipe effect
//Add Below code to App Delegate .m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:14.0f],NSForegroundColorAttributeName : [Utility getUIColorFromHexString:INACTIVE_TAB_TEXT_COLOR]
} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:14.0f],NSForegroundColorAttributeName : [UIColor whiteColor]
} forState:UIControlStateSelected];
[[UITabBar appearance] setBarTintColor:[Utility getUIColorFromHexString:BACKGROUND_PRIMARY_COLOR]];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName,nil]];
return YES;
}
//Add Below code to .h file
#import <UIKit/UIKit.h>
@interface TabControllerViewController : UITabBarController <UITabBarControllerDelegate>
@end
//Start .M File ***************
//Add Below Code to .m file
#import "TabControllerViewController.h"
#import "AppConstants.h"
#import "Utility.h"
#import "ViewController.h"
#import "RetailBranchesViewController.h"
@interface TabControllerViewController (){
UIView *bottomMainView,*bottomBorderView;
}
@end
@implementation TabControllerViewController
int i;
static int BOTTOM_BORDER_HEIGHT = 3;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Set tab bar bottom to top with custom border
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
i++;
NSLog(@"Call viewWillLayoutSubviews: %d",i);
if(i <= 1){
[self.tabBar invalidateIntrinsicContentSize];
CGFloat tabSize = 50.0;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
tabSize = 35.0;
}
CGRect tabFrame = self.tabBar.frame;
tabFrame.size.height = tabSize;
tabFrame.origin.y = self.view.frame.origin.y - 65;
self.tabBar.frame = tabFrame;
// Set the translucent property to NO then back to YES to
// force the UITabBar to reblur, otherwise part of the
// new frame will be completely transparent if we rotate
// from a landscape orientation to a portrait orientation.
bottomMainView = [[UIView alloc]initWithFrame:CGRectMake(0, self.tabBar.frame.size.height - 3, self.tabBar.frame.size.width, BOTTOM_BORDER_HEIGHT)];
[bottomMainView setBackgroundColor:[Utility getUIColorFromHexString:BACKGROUND_PRIMARY_COLOR]];
bottomBorderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tabBar.frame.size.width / 2, BOTTOM_BORDER_HEIGHT)];
[bottomBorderView setBackgroundColor:[UIColor whiteColor]];
[bottomMainView addSubview:bottomBorderView];
bottomBorderView.layer.masksToBounds = NO;
bottomBorderView.layer.shadowOffset = CGSizeMake(0, 1);
bottomBorderView.layer.shadowRadius = 2;
bottomBorderView.layer.shadowOpacity = 0.5;
[self.tabBar addSubview:bottomMainView];
}
}
//Add Swipe Animation Here
- (BOOL) tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController {
NSString *childClassName = NSStringFromClass([viewController class]);
NSString *retailClassName = NSStringFromClass([RetailBranchesViewController class]);
// http://stackoverflow.com/questions/5161730/iphone-how-to-switch-tabs-with-an-animation
NSUInteger controllerIndex = [self.viewControllers indexOfObject:viewController];
if (controllerIndex == tabBarController.selectedIndex) {
return NO;
}
// Get the views.
UIView *fromView = tabBarController.selectedViewController.view;
UIView *toView = [tabBarController.viewControllers[controllerIndex] view];
// Get the size of the view area.
CGRect viewSize = fromView.frame;
BOOL scrollRight = controllerIndex > tabBarController.selectedIndex;
// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];
// Position it off screen.
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
toView.frame = CGRectMake((scrollRight ? screenWidth : -screenWidth), viewSize.origin.y, screenWidth, viewSize.size.height);
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// Animate the views on and off the screen. This will appear to slide.
fromView.frame = CGRectMake((scrollRight ? -screenWidth : screenWidth), viewSize.origin.y, screenWidth, viewSize.size.height);
toView.frame = CGRectMake(0, viewSize.origin.y, screenWidth, viewSize.size.height);
if([childClassName isEqualToString:retailClassName]){
CGRect frame = bottomBorderView.frame;
frame.origin.x = self.tabBar.frame.size.width / 2;
bottomBorderView.frame = frame;
bottomBorderView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
}else{
CGRect frame = bottomBorderView.frame;
frame.origin.x = 0;
bottomBorderView.frame = frame;
bottomBorderView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
}
}
completion:^(BOOL finished) {
if (finished) {
// Remove the old view from the tabbar view.
[fromView removeFromSuperview];
tabBarController.selectedIndex = controllerIndex;
}
}];
return NO;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment