iOS tabbar 中間添加自定義按鈕

在某些項目的初期我們經常會選擇使用UITabbarController或者是UINavigationController或者是兩者的結合,經常需要自定義自己需要的類,本文講述了自定義UITabbarController及向UITabBar中添加自定義按鈕的使用。

首先自定義一個繼承自UITabbarController的類,定義4個屬性,均為UIViewController類型的,作為tabbarController的子視圖,然后分別將他們加入到自定義的UITabbarController的子控制視圖。自定義的UITabbarController命名為WWTTabController。寫一個方法用來做一些初始化的工作,然后再viewDidLoad里面調用方法如下:

#pragma mark 初始化tabBar對應的ViewControllers

-?(void)setControllers{

//配置的各個tabbar對應的controller

self.vc1=?[[UIViewControlleralloc]init];

self.vc1.view.backgroundColor=?[UIColorredColor];

self.vc1.tabBarItem=?[selfitemWithSelectedImage:@"tabBar_me_icon@2x"image:@"tabBar_me_icon@2x"title:@"首頁"];

//配置的各個tabbar對應的controller

self.vc2=?[[UIViewControlleralloc]init];

self.vc2.view.backgroundColor=?[UIColorblueColor];

self.vc2.tabBarItem=?[selfitemWithSelectedImage:@"tabBar_me_icon@2x"image:@"tabBar_me_icon@2x"title:@"消息"];

//配置的各個tabbar對應的controller

self.vc3=?[[UIViewControlleralloc]init];

self.vc3.view.backgroundColor=?[UIColorcyanColor];

self.vc3.tabBarItem=?[selfitemWithSelectedImage:@"tabBar_new_icon@2x"image:@"tabBar_new_icon@2x"title:@"發現"];

//配置的各個tabbar對應的controller

self.vc4=?[[UIViewControlleralloc]init];

self.vc4.view.backgroundColor=?[UIColororangeColor];

self.vc4.tabBarItem=?[selfitemWithSelectedImage:@"tabBar_friendTrends_icon@3x"image:@"tabBar_friendTrends_icon@3x"title:@"我"];

//統一用tabbar來管理navigationController

self.viewControllers=?@[self.vc1,self.vc2,self.vc3,self.vc4];

self.tabBar.tintColor=?[UIColorgreenColor];

self.tabBar.barTintColor=?[UIColorwhiteColor];

}

-?(UITabBarItem*)itemWithSelectedImage:(NSString*)selectImageimage:(NSString*)imagetitle:(NSString*)title{

UIImage*im?=?[[UIImageimageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UITabBarItem*item?=?[[UITabBarItemalloc]initWithTitle:titleimage:imselectedImage:im];

returnitem;

}

然后自定義一個繼承自UITabBar的類,在該類中設置UITabBar的子控件,在該類中定義一個UIButton屬性,放在中間,系統自帶的UIBarButton在兩側,將自定義的UIButton設置成懶加載代碼如下:

//設置自己定義的button為懶加載并且設置上背景圖片

-?(UIButton*)btn{

if(_btn==nil)?{

_btn?=?[UIButtonbuttonWithType:UIButtonTypeCustom];

[_btnsetImage:[UIImageimageNamed:@"tabBar_publish_icon"]forState:UIControlStateNormal];

[_btnsetBackgroundImage:[UIImageimageNamed:@"tabBar_publish_icon"]forState:UIControlStateNormal];

//button的大小與圖片一致

[_btnsizeToFit];

[selfaddSubview:self.btn];

}

return_btn;

}

來一個重新布局的方法//設置tabbar子控件的布局

- (void)layoutSubviews{

CGFloatw =self.bounds.size.width;

CGFloath =self.bounds.size.height;

CGFloatbtnx =0;

CGFloatbtny =0;

//5.0是tabbar中的控件的數量

CGFloatwidth =self.bounds.size.width/5.0;

CGFloatheight =self.bounds.size.height;

inti=0;

for(UIView*btninself.subviews) {

//判斷是否是系統自帶的UITabBarButton類型的控件

if([btnisKindOfClass:NSClassFromString(@"UITabBarButton")]) {

if(i==2) {

i=3;

}

btnx = i*width;

btn.frame=CGRectMake(btnx, btny, width, height);

i++;

}

}

//設置自定義button的位置

self.btn.center=CGPointMake(w*0.5, h*0.5);

}



然后再自定義的UITabbarController的viewDidLoad中定義一個自定義的UITabBar,并且替換掉系統自帶的UITabBar,代碼如下

[superviewDidLoad];

//?Do?any?additional?setup?after?loading?the?view.

WWTTabBar*tabBar?=?[[WWTTabBaralloc]initWithFrame:self.tabBar.frame];

tabBar.backgroundColor=?[UIColorwhiteColor];

//設置tabbar時只能用keyValue方式

[selfsetValue:tabBarforKeyPath:@"tabBar"];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容