github地址:https://github.com/ChenYilong/CYLTabBarController
在oc中引用: 创建TabBarViewController
在.h文件中:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "CYLTabBarController.h"
@interface TabBarViewController : NSObject
@property (nonatomic, readonly, strong) CYLTabBarController *tabBarController;
@end
在.m文件中:
#import "TabBarViewController.h"
#import "HomePageVC.h"
#import "MessageVCs.h"
#import "GoodsShopVC.h"
#import "GoodsClassVC.h"
#import "SelfViewController.h"
#import "SelfVCViewController.h"
#import "NavViewController.h"
#import "CYLTabBarController.h"
@interface TabBarViewController ()
@property (nonatomic, readwrite, strong) CYLTabBarController *tabBarController;
@end
@implementation TabBarViewController
- (CYLTabBarController *)tabBarController{
if (_tabBarController == nil) {
HomePageVC * homepage = [[HomePageVC alloc]init];
MessageVCs * message = [[MessageVCs alloc]init];
GoodsClassVC * goodsclass = [[GoodsClassVC alloc]init];
GoodsShopVC * goodsshop = [[GoodsShopVC alloc]init];
SelfVCViewController * selfvc = [[SelfVCViewController alloc]init];
NavViewController * homeNav = [[NavViewController alloc]initWithRootViewController:homepage];
NavViewController * messageNav = [[NavViewController alloc]initWithRootViewController:message];
NavViewController * classNav = [[NavViewController alloc]initWithRootViewController:goodsclass];
NavViewController * shopNav = [[NavViewController alloc]initWithRootViewController:goodsshop];
NavViewController * selfNav = [[NavViewController alloc]initWithRootViewController:selfvc];
CYLTabBarController * tabBarController = [[CYLTabBarController alloc]init];
[self setUpTabBarItemsAttributesForController:tabBarController];
[tabBarController setViewControllers:@[homeNav,messageNav,classNav,shopNav,selfNav]];
[[self class]customizeTabBarAppearance];
_tabBarController = tabBarController;
}
return _tabBarController;
}
+ (void)customizeTabBarAppearance {
//去除 TabBar 自带的顶部阴影
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
// set the text color for unselected state
// 普通状态下的文字属性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
normalAttrs[NSForegroundColorAttributeName] = HOMEGOOD_BROWN;
// set the text color for selected state
// 选中状态下的文字属性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSForegroundColorAttributeName] = HOMEGOOD_BROWN;
// set the text Attributes
// 设置文字属性
UITabBarItem *tabBar = [UITabBarItem appearance];
[tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
// Set the dark color to selected tab (the dimmed background)
// TabBarItem选中后的背景颜色
// [[UITabBar appearance] setSelectionIndicatorImage:[self imageFromColor:[UIColor colorWithRed:26 / 255.0 green:163 / 255.0 blue:133 / 255.0 alpha:1] forSize:CGSizeMake([UIScreen mainScreen].bounds.size.width / 5.0f, 49) withCornerRadius:0]];
// set the bar background color
// 设置背景图片
UITabBar *tabBarAppearance = [UITabBar appearance];
[tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbg"]];
}
+ (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius {
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContext(size);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
// Draw your image
[image drawInRect:rect];
// Get the image, here setting the UIImageView image
image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return image;
}
- (void)setUpTabBarItemsAttributesForController:(CYLTabBarController *)tabBarController {
NSArray * images = @[@"shouye01",@"xiaoxi01",@"fenlei01",@"goodshop01",@"wode01"];
NSArray * imageSel = @[@"shouye02",@"xiaoxi02",@"fenlei02",@"goodshop02",@"wode02"];
NSArray * titleArr = @[@"首页",@"消息",@"分类",@"购物车",@"我的"];
NSMutableArray *tabBarItemsAttributes = [NSMutableArray array];
for (int i = 0 ; i < 5 ; i++) {
NSDictionary *dict = @{
CYLTabBarItemTitle : titleArr[i],
CYLTabBarItemImage : images[i],
CYLTabBarItemSelectedImage : imageSel[i],
};
[tabBarItemsAttributes addObject:dict];
}
tabBarController.tabBarItemsAttributes = tabBarItemsAttributes;
}
@end
因需要点击tabBarItem时做跳转判断,在CYLTabBarController.m中重写方法- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if (![viewController.tabBarItem.title isEqualToString:@"首页"] ) {
if ([Tools isLogin]) {
return YES;
}else{
LoginVC * vc = [[LoginVC alloc]init];
vc.hidesBottomBarWhenPushed = YES;
[((UINavigationController*)tabBarController.selectedViewController) pushViewController:vc animated:YES];
return NO;
}
}
return YES;
}
TabBarViewController * tabCtr = [[TabBarViewController alloc]init];
UIWindow *window =[[[UIApplication sharedApplication] windows] objectAtIndex:0];
window.rootViewController = tabCtr.tabBarController;
tabCtr.tabBarController.selectedIndex = tabCtr.tabBarController.viewControllers.count - 1;
在Swift中引用CYLTabBarController:
在 Bridging-Header.h中
#import "CYLTabBarController/CYLTabBar.h"
import UIKit
import CYLTabBarController
class RootTabViewController: CYLTabBarController {
let titles = ["首页","我的"]
let selectedImages = ["tabbar0Sel","tabbar1Sel"]
let images = ["tabbar0","tabbar1"]
override func viewDidLoad() {
super.viewDidLoad()
let first = FirstViewController()
let firstNav = NavViewController(rootViewController: first)
let second = SecondViewController()
let secondNav = NavViewController(rootViewController: second)
let vcs = [firstNav,secondNav]
var tabBarItemsAttributes:[AnyObject] = []
var viewControllers:[AnyObject] = []
for i in 0 ... (titles.count - 1) {
let dict:[NSObject:AnyObject] = [CYLTabBarItemTitle:titles[i],CYLTabBarItemImage:images[i],CYLTabBarItemSelectedImage:selectedImages[i]]
let vc = vcs[i]
tabBarItemsAttributes.append(dict)
viewControllers.append(vc)
}
customizeTabBarAppearance()
self.tabBarItemsAttributes = tabBarItemsAttributes
self.viewControllers = viewControllers
}
func customizeTabBarAppearance (){
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:rgba(72, g: 153, b: 222, a: 1.0)], forState: .Selected)
}
}