Skip to content

Instantly share code, notes, and snippets.

@z5ottu
Created February 11, 2016 09:41
Show Gist options
  • Save z5ottu/9b219e8de4dac253637d to your computer and use it in GitHub Desktop.
Save z5ottu/9b219e8de4dac253637d to your computer and use it in GitHub Desktop.
//
// CustomTab.m
//
// Created by z5ottu Szloboda Zsolt on 10/02/16.
// Copyright © 2016 z5 tech. All rights reserved.
//
// compatibility: ios8, ios9
#import <Foundation/Foundation.h>
#import "CustomTab.h"
@interface CustomTab ()
@end
@implementation CustomTab
@synthesize tabBar;
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
[UITabBar appearance].tintColor = [UIColor whiteColor];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
UIImage* lightBackground = [self imageFromColor:UIColorFromRGB(0xabcc58) setwidth:tabBar.frame.size.width setheight:tabBar.frame.size.height];
[tabBar setBackgroundImage:lightBackground];
UIImage* darkImage = [self imageFromColor:UIColorFromRGB(0x829d3c) setwidth:tabBar.frame.size.width/tabBar.items.count setheight:tabBar.frame.size.height];
[tabBar setSelectionIndicatorImage:darkImage];
for (UITabBarItem *item in tabBar.items){
if (item.image != nil){
CGRect rect = CGRectMake(0, 0, item.image.size.width, item.image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, item.image.CGImage);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
item.image = [[UIImage imageWithCGImage:img.CGImage
scale:1.0 orientation: UIImageOrientationDownMirrored] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
}
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController{
NSUInteger index = [[tabBarController viewControllers] indexOfObject:viewController];
if (index >0){
return true;
}else{
return true;
}
}
- (UIImage *)imageFromColor:(UIColor *)color setwidth:(CGFloat)width setheight:(CGFloat)height {
CGRect rect = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment