用的时候直接拷贝代码即可.
1.在AppDelegate设置跟控制器为:PQTabBarController
#import "PQTabBarController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1.创建window self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; // 2.创建根控制器 PQTabBarController *tabBarVc = [[PQTabBarController alloc]init]; self.window.rootViewController = tabBarVc; // 3.显示window [self.window makeKeyAndVisible]; return YES; }
2.自定义TabBar, PQTabBar.h文件:
#import <UIKit/UIKit.h> @class PQTabBar; @protocol PQTabBarDelegate <NSObject> - (void)tabBar:(PQTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to; @end @interface PQTabBar : UIView @property(nonatomic,weak)id<PQTabBarDelegate>delegate; - (void)addTabBarButtonWith:(UITabBarItem *)item; @end
PQTabBar.m文件:
#import "PQTabBar.h" #import "PQTaBarButton.h" @interface PQTabBar() @property (nonatomic,weak)UIButton * currentSelectedBtn; @end @implementation PQTabBar - (void)addTabBarButtonWith:(UITabBarItem *)item{ // 创建按钮 PQTaBarButton *btn = [PQTaBarButton buttonWithType:UIButtonTypeCustom]; [self addSubview:btn]; // 设置图片 [btn setBackgroundImage:item.image forState:UIControlStateNormal]; [btn setBackgroundImage:item.selectedImage forState:UIControlStateSelected]; // 监听点击事件 [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown]; // 设置默认选中的第0个按钮 NSInteger count = self.subviews.count; if(1 == count){ [self btnClick:btn]; } } - (void)layoutSubviews { NSInteger count = self.subviews.count; CGFloat btnW = self.frame.size.width/5; CGFloat btnH = self.frame.size.height; for (int i = 0; i < count; i++) { // 1.取出对应的按钮 UIButton *btn = self.subviews[i]; // 2.计算frame CGFloat btnY = 0; CGFloat btnX = btnW * i; // 3.设置frame btn.frame = CGRectMake(btnX, btnY, btnW, btnH); // 4.设置按钮的tag btn.tag = i; } } - (void)btnClick:(UIButton *)btn { //1.通知代理 if([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]){ [self.delegate tabBar:self didSelectedButtonFrom:self.currentSelectedBtn.tag to:btn.tag]; } // 取消上一次选中 self.currentSelectedBtn.selected = NO; // 设置按钮为选中 btn.selected = YES; // 记录当前选中的按钮 self.currentSelectedBtn = btn; } @end
3.自定义TabBarItem. PQTaBarButton.h文件:
#import <UIKit/UIKit.h> @interface PQTaBarButton : UIButton @end
PQTaBarButton.m文件中:
#import "PQTaBarButton.h" @implementation PQTaBarButton - (void)setHighlighted:(BOOL)highlighted{ } @end
4.自定义UITabBarController, PQTabBarController.h文件
#import <UIKit/UIKit.h> @interface PQTabBarController : UITabBarController @end
PQTabBarController.m文件
#import "PQTabBarController.h" #import "PQGameViewController.h" #import "PQMenuViewController.h" #import "PQMessageViewController.h" #import "PQCommunityViewController.h" #import "PQReservationViewController.h" #import "PQTabBar.h" #import "PQTaBarButton.h" @interface PQTabBarController ()<PQTabBarDelegate> @property (nonatomic,weak)PQTabBar * customTabBar; @end @implementation PQTabBarController - (void)viewDidLoad { [super viewDidLoad]; // 2.添加自定义的tabBar [self setupTabBar]; //添加子控制器 [self setUpAllChlidVc]; } - (void)setupTabBar { // 1.添加自定义 PQTabBar *cutomTabBar = [[PQTabBar alloc]init]; cutomTabBar.backgroundColor = [UIColor greenColor]; cutomTabBar.frame = self.tabBar.frame; self.customTabBar = cutomTabBar; // 成为自定义tabBar的代理 cutomTabBar.delegate = self; [self.view addSubview:cutomTabBar]; // 2.删除系统自带 [self.tabBar removeFromSuperview]; } - (void)setUpAllChlidVc { PQGameViewController *gameVc = (PQGameViewController *)[self addChildVcWithName:@"PQGameViewController" title:@"游戏" image:@"TabBar1" selectedImage:@"avater"]; PQMenuViewController *menuVc = (PQMenuViewController *)[self addChildVcWithName:@"PQMenuViewController" title:@"菜单" image:@"TabBar2" selectedImage:@"avater"]; PQMessageViewController *messageVc = (PQMessageViewController *)[self addChildVcWithName:@"PQMessageViewController" title:@"消息" image:@"TabBar3" selectedImage:@"avater"]; PQCommunityViewController *communityVc = (PQCommunityViewController *)[self addChildVcWithName:@"PQCommunityViewController" title:@"社区" image:@"TabBar4" selectedImage:@"avater"]; PQReservationViewController *reservationVc = (PQReservationViewController *)[self addChildVcWithName:@"PQReservationViewController" title:@"待选" image:@"TabBar5" selectedImage:@"avater"]; self.viewControllers = @[gameVc , menuVc , messageVc , communityVc , reservationVc]; } - (UIViewController *)addChildVcWithName:(NSString *)vcName title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage { UIStoryboard *sb = [UIStoryboard storyboardWithName:vcName bundle:nil]; UIViewController *Vc = sb.instantiateInitialViewController; //设置对应控制器按钮的图片 Vc.tabBarItem.image = [UIImage imageNamed:image]; Vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];// 调用自定义TabBar创建按钮的方法 [self.customTabBar addTabBarButtonWith:Vc.tabBarItem]; return Vc; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark 代理方法 - (void)tabBar:(PQTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to{ self.selectedIndex = to; } @end