第一種:利用系統自帶的tabbarItem加一個自定義按鈕:
#import "SZMTabBarController.h" #import "SZMTabBar.h" #import "SZMHomeViewCtrl.h" #import "SZMNavigationController.h" #import "SZMDiscoerViewCtrl.h" @interface SZMTabBarController ()<SZMTabBarDelegate> @end @implementation SZMTabBarController - (void)viewDidLoad { [super viewDidLoad]; //初始化一個自己的tabbar SZMTabBar *tabBar = [[SZMTabBar alloc]init]; tabBar.delegate = self; //通過KVC去設置只讀的屬性 [self setValue:tabBar forKey:@"tabBar"]; //添加四個控制器 //首頁 SZMHomeViewCtrl *homeCtrl = [[SZMHomeViewCtrl alloc]init]; [self addChildVc:homeCtrl title:@"首頁" imageName:@"tabbar_home" selImgName:@"tabbar_home_selected"]; UITableViewController *messageCtrl = [[UITableViewController alloc]init]; [self addChildVc:messageCtrl title:@"消息" imageName:@"tabbar_message_center" selImgName:@"tabbar_message_center_selected"]; SZMDiscoerViewCtrl *discoveryCtrl = [[SZMDiscoerViewCtrl alloc]init]; [self addChildVc:discoveryCtrl title:@"發現" imageName:@"tabbar_discover" selImgName:@"tabbar_discover_selected"]; UITableViewController *profileCtrl = [[UITableViewController alloc]init]; [self addChildVc:profileCtrl title:@"我" imageName:@"tabbar_profile" selImgName:@"tabbar_profile_selected"]; } //設置tabbar的一些屬性 - (void)addChildVc:(UIViewController *)Controller title:(NSString *)title imageName:(NSString *)imgName selImgName:(NSString *)selImgName{ Controller.title = title; Controller.tabBarItem.image = [UIImage imageNamed:imgName]; Controller.tabBarItem.selectedImage = [UIImage imageNamed:selImgName]; self.tabBar.tintColor = [UIColor orangeColor]; SZMNavigationController *navCtrl = [[SZMNavigationController alloc]initWithRootViewController:Controller]; [self addChildViewController:navCtrl]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)TabBar:(SZMTabBar *)TabBar plusBtnDidClick:(UIButton *)btn{ NSLog(@"1"); } @end
#import <UIKit/UIKit.h> @class SZMTabBar; #warning 我們自定義的控件,如果是繼承於系統的控件的話,而且系統的控件有代理,我們的協議一定要繼承父類的協議 @protocol SZMTabBarDelegate <NSObject,UITabBarDelegate> - (void)TabBar:(SZMTabBar *)TabBar plusBtnDidClick:(UIButton *)btn; @end @interface SZMTabBar : UITabBar @property (nonatomic,weak) id<SZMTabBarDelegate> delegate; @end
#import "SZMTabBar.h" @interface SZMTabBar () @property (nonatomic,weak) UIButton *plusBtn; @end @implementation SZMTabBar @dynamic delegate; - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //添加加號按鈕 UIButton *plusBtn = [[UIButton alloc]init]; //給button設置不同狀態下背景圖片 [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal]; [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted]; [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal]; [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted]; [self addSubview:plusBtn]; self.plusBtn = plusBtn; plusBtn.size = plusBtn.currentBackgroundImage.size; //給按鈕添加點擊事件 [plusBtn addTarget:self action:@selector(plusBtnClick:) forControlEvents:UIControlEventTouchUpInside]; } return self; } - (void)plusBtnClick:(UIButton *)btn{ //代理方法的實現 if ([self.delegate respondsToSelector:@selector(TabBar:plusBtnDidClick:)]) { [self.delegate TabBar:self plusBtnDidClick:btn]; } } -(void)layoutSubviews{ [super layoutSubviews]; //調整加號按鈕的位置 self.plusBtn.centerX = self.width *0.5; self.plusBtn.centerY = self.height *0.5; //調整uitabbarbutton的大小和位置 CGFloat tabBarBtnW = self.width *0.2; NSInteger index = 0; for (UIView *view in self.subviews) { if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) { view.width = tabBarBtnW; view.x = index * tabBarBtnW; if (index == 1) { index++; } index++; } } } @end
第二種:自己完全自定義底部tabbar:
#import "SZMMainTabBarController.h" #import "SZMBottomBarView.h" @interface SZMMainTabBarController ()<SZMBottomBarViewDelegate> @end @implementation SZMMainTabBarController - (void)viewDidLoad { [super viewDidLoad]; //加載子控制器 [self loadSubControllers]; //加載底部自定義view [self loadBottomView]; } //加載底部自定義的view - (void)loadBottomView{ //創建底部的自定義的tabbarview SZMBottomBarView *BottomView = [[SZMBottomBarView alloc]init]; BottomView.delegate = self; BottomView.backgroundColor = [UIColor redColor]; BottomView.frame = self.tabBar.bounds; [self.tabBar addSubview:BottomView]; //給自定義view里添加按鈕 for (int i = 0; i < self.viewControllers.count; i++) { NSString *NormalImgName = [NSString stringWithFormat:@"TabBar%d",i+1]; NSString *SelectedImgName = [NSString stringWithFormat:@"TabBar%dSel",i+1]; [BottomView addBarButtonWithNormalImgName:NormalImgName andSelectedImgName:SelectedImgName]; } } - (void)SZMBottomBarView:(SZMBottomBarView *)SZMBottomBarViewWith didSclectedBtnIndex:(int)index{ self.selectedIndex = index; } //加載五個子控制器到tabbar控制器 - (void)loadSubControllers{ //購彩大廳 UINavigationController *navHall = [self navigationControllerWithStoryboardName:@"Hall"]; //競技場 UINavigationController *navArena = [self navigationControllerWithStoryboardName:@"Arena"]; //發現 UINavigationController *navDiscovery = [self navigationControllerWithStoryboardName:@"Discovery"]; //開獎信息 UINavigationController *navHistory = [self navigationControllerWithStoryboardName:@"History"]; //我的彩票 UINavigationController *navMyLottery = [self navigationControllerWithStoryboardName:@"MyLottery"]; self.viewControllers = @[navHall,navArena,navDiscovery,navHistory,navMyLottery]; } //加載對應的初始化控制器的方法 - (UINavigationController *)navigationControllerWithStoryboardName:(NSString *)storyboard{ //1.創建stroyboard對象 UIStoryboard *S1 = [UIStoryboard storyboardWithName:storyboard bundle:nil]; //2.實例化這個stroyboard中的初始化控制器 UINavigationController *nav = [S1 instantiateInitialViewController]; return nav; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @class SZMBottomBarView; @protocol SZMBottomBarViewDelegate <NSObject> @optional - (void)SZMBottomBarView:(SZMBottomBarView *)SZMBottomBarViewWith didSclectedBtnIndex:(int)index; @end @interface SZMBottomBarView : UIView - (void)addBarButtonWithNormalImgName:(NSString *)NormalImgName andSelectedImgName:(NSString *)SelectedImgName; @property (nonatomic,weak) id<SZMBottomBarViewDelegate> delegate; @end
#import "SZMBottomBarView.h" #import "SZMBottomBarBtn.h" @interface SZMBottomBarView () @property (nonatomic,weak) UIButton *selectedBtn; @end @implementation SZMBottomBarView - (void)addBarButtonWithNormalImgName:(NSString *)NormalImgName andSelectedImgName:(NSString *)SelectedImgName { //創建一個uibutton SZMBottomBarBtn *btn = [[SZMBottomBarBtn alloc]init]; //設置uibutton的背影圖片 UIImage *NorImg = [UIImage imageNamed:NormalImgName]; UIImage *SelImg = [UIImage imageNamed:SelectedImgName]; [btn setBackgroundImage:NorImg forState:UIControlStateNormal]; [btn setBackgroundImage:SelImg forState:UIControlStateSelected]; //把uibutton添加到自己身上 [self addSubview:btn]; //給每個底部的按鈕添加點擊事件 [btn addTarget:self action:@selector(bottomBarButtonTouchDown:) forControlEvents:UIControlEventTouchDown]; } //底部按鈕點擊事件的方法(占擊哪個把哪個設置為選中狀態) - (void)bottomBarButtonTouchDown:(UIButton *)sennder { //設置被點擊按鈕的狀態 self.selectedBtn.selected = NO; sennder.selected = YES; self.selectedBtn = sennder; //切換控制器 if ([self.delegate respondsToSelector:@selector(SZMBottomBarView:didSclectedBtnIndex:)]) { [self.delegate SZMBottomBarView:self didSclectedBtnIndex:(int)sennder.tag]; } } - (void)layoutSubviews { [super layoutSubviews]; CGFloat y = 0; CGFloat w = self.bounds.size.width / self.subviews.count; CGFloat h = self.bounds.size.height; for (int i = 0; i < self.subviews.count; ++i) { CGFloat X = w *i; UIButton *button = (UIButton *)self.subviews[i]; button.tag = i; button.frame = CGRectMake(X, y, w, h); //設置進入頁面后第一個按鈕為選中狀態 if (i == 0) { self.selectedBtn.selected = NO; button.selected = YES; self.selectedBtn = button; } } } @end
#import <UIKit/UIKit.h> @interface SZMBottomBarBtn : UIButton @end
#import "SZMBottomBarBtn.h" @implementation SZMBottomBarBtn - (void)setHighlighted:(BOOL)highlighted{ } @end