自定義UITabBar替換系統默認的,目的是為了在UITabBar中間位置添加一個“+號按鈕”,下面我們來聊聊具體的實現。
1、自定義WBTabBar,讓其繼承自UITabBar,代碼如下:
// // WBTabBar.h // SinaWeibo // // Created by android_ls on 15/5/21. // Copyright (c) 2015年 android_ls. All rights reserved. // #import <UIKit/UIKit.h> @interface WBTabBar : UITabBar @end
2、tabBar是UITabBarController的只讀成員變量(屬性),是不讓修改的,在UITabBarController.h文件中的聲明如下:
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0);
針對於這種情況,我們可以使用KVC的方式,更換系統自帶的UITabBar,實現代碼如下:
WBTabBar *tabBar = [[WBTabBar alloc] init]; [self setValue:tabBar forKeyPath:@"tabBar"];
3、添加一個UIButton到WBTabBar中,實現代碼如下:
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 添加一個按鈕到tabbar中 UIButton *plusBtn = [[UIButton alloc] init]; [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal]; [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted]; [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_add"] forState:UIControlStateNormal]; [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted]; plusBtn.size = plusBtn.currentBackgroundImage.size; [plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:plusBtn]; self.plusBtn = plusBtn; } return self; }
4、設置加號按鈕的位置,調整WBTabBar中各個UITabBarButton的位置和寬度,具體實現代碼如下:
- (void)layoutSubviews { [super layoutSubviews]; // 1.設置加號按鈕的位置 self.plusBtn.centerX = self.width * 0.5; self.plusBtn.centerY = self.height * 0.5; // 2.設置其它UITabBarButton的位置和尺寸 CGFloat tabbarButtonW = self.width / 5; CGFloat tabbarButtonIndex = 0; for (UIView *child in self.subviews) { Class class = NSClassFromString(@"UITabBarButton"); if ([child isKindOfClass:class]) { // 設置寬度 child.width = tabbarButtonW; // 設置x child.x = tabbarButtonIndex * tabbarButtonW; // 增加索引 tabbarButtonIndex++; if (tabbarButtonIndex == 2) { tabbarButtonIndex++; } } } }
5、定義WBTabBarDelegate協議,聲明WBTabBar的代理,代碼如下:
// // WBTabBar.h // SinaWeibo // // Created by android_ls on 15/5/21. // Copyright (c) 2015年 android_ls. All rights reserved. // #import <UIKit/UIKit.h> #pragma mark 因為在UITabBar中已經聲明過一個UITabBarDelegate協議, #pragma mark 我們若想新增一個對外的代理函數,可以讓我們自定義的協議繼承自UITabBarDelegate,添加一個擴展函數。 @class WBTabBar; @protocol WBTabBarDelegate <UITabBarDelegate> @optional - (void)tabBarDidClickPlusButton:(WBTabBar *)tabBar; @end @interface WBTabBar : UITabBar @property (nonatomic, weak) id<WBTabBarDelegate> tabBarDelegate; @end
6、在加號按鈕的點擊事件處理器中,通知代理
#pragma mark 加號按鈕點擊事件處理器 - (void)plusClick { // 通知代理 if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) { [self.tabBarDelegate tabBarDidClickPlusButton:self]; } }
7、在WBTabBarController中設置WBTabBar的代理,具體實現如下:
// 2、使用KVC的方式,更換系統自帶的UITabBar WBTabBar *tabBar = [[WBTabBar alloc] init]; tabBar.tabBarDelegate = self; [self setValue:tabBar forKeyPath:@"tabBar"];
#pragma mark - HWTabBarDelegate代理方法 - (void)tabBarDidClickPlusButton:(WBTabBar *)tabBar { ComposeViewController *composeViewController= [[ComposeViewController alloc] init]; UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:composeViewController]; [self presentViewController:navigationController animated:YES completion:nil]; }