標籤:
自訂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];}
iOS之自訂UITabBar替換系統預設的(添加“+”號按鈕)