在某些項(xiàng)目的初期我們經(jīng)常會選擇使用UITabbarController或者是UINavigationController或者是兩者的結(jié)合什黑,經(jīng)常需要自定義自己需要的類煞肾,本文講述了自定義UITabbarController及向UITabBar中添加自定義按鈕的使用热监。
首先自定義一個繼承自UITabbarController的類,定義4個屬性钻趋,均為UIViewController類型的弹灭,作為tabbarController的子視圖,然后分別將他們加入到自定義的UITabbarController的子控制視圖辜限。自定義的UITabbarController命名為WWTTabController皇拣。寫一個方法用來做一些初始化的工作,然后再viewDidLoad里面調(diào)用方法如下:
#pragma mark 初始化tabBar對應(yīng)的ViewControllers
-?(void)setControllers{
//配置的各個tabbar對應(yīng)的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對應(yīng)的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對應(yīng)的controller
self.vc3=?[[UIViewControlleralloc]init];
self.vc3.view.backgroundColor=?[UIColorcyanColor];
self.vc3.tabBarItem=?[selfitemWithSelectedImage:@"tabBar_new_icon@2x"image:@"tabBar_new_icon@2x"title:@"發(fā)現(xiàn)"];
//配置的各個tabbar對應(yīng)的controller
self.vc4=?[[UIViewControlleralloc]init];
self.vc4.view.backgroundColor=?[UIColororangeColor];
self.vc4.tabBarItem=?[selfitemWithSelectedImage:@"tabBar_friendTrends_icon@3x"image:@"tabBar_friendTrends_icon@3x"title:@"我"];
//統(tǒng)一用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的類薄嫡,在該類中設(shè)置UITabBar的子控件氧急,在該類中定義一個UIButton屬性,放在中間毫深,系統(tǒng)自帶的UIBarButton在兩側(cè)吩坝,將自定義的UIButton設(shè)置成懶加載代碼如下:
//設(shè)置自己定義的button為懶加載并且設(shè)置上背景圖片
-?(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;
}
來一個重新布局的方法//設(shè)置tabbar子控件的布局
- (void)layoutSubviews{
CGFloatw =self.bounds.size.width;
CGFloath =self.bounds.size.height;
CGFloatbtnx =0;
CGFloatbtny =0;
//5.0是tabbar中的控件的數(shù)量
CGFloatwidth =self.bounds.size.width/5.0;
CGFloatheight =self.bounds.size.height;
inti=0;
for(UIView*btninself.subviews) {
//判斷是否是系統(tǒng)自帶的UITabBarButton類型的控件
if([btnisKindOfClass:NSClassFromString(@"UITabBarButton")]) {
if(i==2) {
i=3;
}
btnx = i*width;
btn.frame=CGRectMake(btnx, btny, width, height);
i++;
}
}
//設(shè)置自定義button的位置
self.btn.center=CGPointMake(w*0.5, h*0.5);
}
然后再自定義的UITabbarController的viewDidLoad中定義一個自定義的UITabBar,并且替換掉系統(tǒng)自帶的UITabBar哑蔫,代碼如下
[superviewDidLoad];
//?Do?any?additional?setup?after?loading?the?view.
WWTTabBar*tabBar?=?[[WWTTabBaralloc]initWithFrame:self.tabBar.frame];
tabBar.backgroundColor=?[UIColorwhiteColor];
//設(shè)置tabbar時只能用keyValue方式
[selfsetValue:tabBarforKeyPath:@"tabBar"];