代碼:參考黑馬明杰的微博項(xiàng)目中的自定義bar捉腥,加入一點(diǎn)自己的思路
一、思路講解
自定義tabbar替換定義的tabbar你画,在自定義tabbar內(nèi)調(diào)節(jié)相關(guān)按鈕的位置及樣式抵碟,調(diào)節(jié)響應(yīng)鏈的點(diǎn)擊方法
二、相關(guān)問(wèn)題解決
1坏匪、自定義tabbar繼承UITabBar在layoutSubviews方法中調(diào)節(jié)中間按鈕的位置拟逮,將位置往上調(diào)節(jié)。
- (void)layoutSubviews
{
#warning [super layoutSubviews] 一定要調(diào)用
[super layoutSubviews];
// 1.設(shè)置加號(hào)按鈕的位置
// self.plusBtn.centerX = self.width * 0.5;
// self.plusBtn.y = -2;
// 2.設(shè)置其他tabbarButton的位置和尺寸
// CGFloat tabbarButtonW = self.width / 5;
int tabbarButtonIndex = 0;
for (UIView *child in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([child isKindOfClass:class]) {
// // 設(shè)置寬度
// child.width = tabbarButtonW;
// // 設(shè)置x
// child.x = tabbarButtonIndex * tabbarButtonW;
//
// // 增加索引
if (tabbarButtonIndex == 2) {
// [self bringSubviewToFront:child];
child.height = 55;
child.y = -6;
}
tabbarButtonIndex++;
}
}
}
2适滓、分隔線解決
但是分隔線會(huì)在最上層顯示很丑敦迄。解決方法將分隔線隱藏,自己寫(xiě)一個(gè)分隔線加上去凭迹,就不會(huì)擋住凸起的部分罚屋,記得中間的圖標(biāo)不要透明的
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//設(shè)置背景,去除tabbar的黑線
[self setShadowImage:[HXBColor imageFromColor:[UIColor clearColor]]];
[self setBackgroundImage:[HXBColor imageFromColor:[UIColor whiteColor]]];
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
lineView.backgroundColor = kLineMainColor;
[self addSubview:lineView];
self.alpha = 1;
}
return self;
}
3、響應(yīng)鏈解決
突出部分超出了tabbar的范圍點(diǎn)擊上方?jīng)]有效果需要調(diào)節(jié)響應(yīng)鏈方法
根據(jù)范圍自己設(shè)置
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL result = [super pointInside:point withEvent:event];
if (result) {
return result;
}
if (self.plusBtn.x<point.x&&(self.plusBtn.x+self.plusBtn.width)>point.x&&self.plusBtn.y<point.y&&(self.plusBtn.y+self.plusBtn.height)>point.y ) {
return YES;
}
return NO;
}
4嗅绸、使用方法
自定義好tabbar后脾猛,替換原來(lái)的tabbar
// 2.更換系統(tǒng)自帶的tabbar
HWTabBar *tabBar = [[HWTabBar alloc] init];
[self setValue:tabBar forKeyPath:@"tabBar"];
三、示例代碼
@interface HXBTabbarControlle ()<UIAlertViewDelegate>
@property(nonatomic,strong)HXBNavigationController *managerNaviVC;
@end
@implementation HXBTabbarControlle
-(UIStatusBarStyle)preferredStatusBarStyle
{
self.view.backgroundColor = kBackgroundGrayDefautColor;
return UIStatusBarStyleLightContent; //默認(rèn)的值是黑色的
}
+(void)initialize
{
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:kNavBarMainColor,NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateSelected];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:SHColor(109, 109, 109),NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
UITabBar *bar = [UITabBar appearance];
bar.barTintColor = [UIColor whiteColor];
// HWTabBar *bar = [HWTabBar appearance];
// bar.barTintColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// HomeVC *home = [HomeVC new];
[self loginVertify];//登錄驗(yàn)證
SearchHotelMainVC *home = [SearchHotelMainVC new];
[self addChildVc:home title:@"預(yù)定" imageName:@"btn_zhu" selectImageName:@"btn_zhu_h"];
OrderMainVC *order = [OrderMainVC new];
[self addChildVc:order title:@"訂單" imageName:@"btn_dd" selectImageName:@"btn_dd_h"];
OpenDoorCenterVC *door = [OpenDoorCenterVC new];
[self addChildVc:door title:@"自助入住" imageName:@"tab_km" selectImageName:@"tab_km_h"];
ManagerServeVC *manager = [ManagerServeVC new];
[self addChildVc:manager title:@"服務(wù)" imageName:@"tab_fw" selectImageName:@"tab_fw_h"];
MineVC *mine = [MineVC new];
[self addChildVc:mine title:@"我的" imageName:@"btn-w" selectImageName:@"btn-w_h"];
// 2.更換系統(tǒng)自帶的tabbar
HWTabBar *tabBar = [[HWTabBar alloc] init];
[self setValue:tabBar forKeyPath:@"tabBar"];
}
-(void)addChildVc:(UIViewController*)childVC title:(NSString*)title imageName:(NSString*)imageName selectImageName:(NSString*)selectImageName
{
childVC.title = title;
childVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
HXBNavigationController *nav = [[HXBNavigationController alloc] initWithRootViewController:childVC];
[self addChildViewController:nav];
}
自定義tabbar的代碼
#import <UIKit/UIKit.h>
@class HWTabBar;
#warning 因?yàn)镠WTabBar繼承自UITabBar鱼鸠,所以稱為HWTabBar的代理猛拴,也必須實(shí)現(xiàn)UITabBar的代理協(xié)議
@protocol HWTabBarDelegate <UITabBarDelegate>
@optional
- (void)tabBarDidClickPlusButton:(UIButton *)plusBtn;
@end
@interface HWTabBar : UITabBar
@property (nonatomic, weak) id<HWTabBarDelegate> delegate;
@end
#import "HWTabBar.h"
#import "HXBColor.h"
#import "TabbarButton.h"
@interface HWTabBar()
@property (nonatomic, weak) UIButton *plusBtn;
@end
@implementation HWTabBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//設(shè)置背景,去除tabbar的黑線
[self setShadowImage:[HXBColor imageFromColor:[UIColor clearColor]]];
[self setBackgroundImage:[HXBColor imageFromColor:[UIColor whiteColor]]];
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
lineView.backgroundColor = kLineMainColor;
[self addSubview:lineView];
self.alpha = 1;
}
return self;
}
//通知方法
-(void)upDatePlusBtn
{
self.plusBtn.selected = YES;
}
-(void)dealloc
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:kNotificationKey object:nil];
}
/**
* 加號(hào)按鈕點(diǎn)擊
*/
- (void)plusClick
{
// 通知代理
if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
[self.delegate tabBarDidClickPlusButton:self.plusBtn];
}
}
- (void)layoutSubviews
{
#warning [super layoutSubviews] 一定要調(diào)用
[super layoutSubviews];
// 1.設(shè)置加號(hào)按鈕的位置
// self.plusBtn.centerX = self.width * 0.5;
// self.plusBtn.y = -2;
// 2.設(shè)置其他tabbarButton的位置和尺寸
// CGFloat tabbarButtonW = self.width / 5;
int tabbarButtonIndex = 0;
for (UIView *child in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([child isKindOfClass:class]) {
// // 設(shè)置寬度
// child.width = tabbarButtonW;
// // 設(shè)置x
// child.x = tabbarButtonIndex * tabbarButtonW;
//
// // 增加索引
if (tabbarButtonIndex == 2) {
// [self bringSubviewToFront:child];
child.height = 55;
child.y = -6;
}
tabbarButtonIndex++;
}
}
}
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL result = [super pointInside:point withEvent:event];
if (result) {
return result;
}
if (self.plusBtn.x<point.x&&(self.plusBtn.x+self.plusBtn.width)>point.x&&self.plusBtn.y<point.y&&(self.plusBtn.y+self.plusBtn.height)>point.y ) {
return YES;
}
return NO;
}
@end