在某些项目的初期我们经常会选择使用UITabbarController或者是UINavigationController或者是两者的结合,经常需要自定义自己需要的类,本文讲述了自定义UITabbarController及向UITabBar中添加自定义按钮的使用。
首先自定义一个继承自UITabbarController的类,定义4个属性,均为UIViewController类型的,作为tabbarController的子视图,然后分别将他们加入到自定义的UITabbarController的子控制视图。自定义的UITabbarController命名为WWTTabController。写一个方法用来做一些初始化的工作,然后再viewDidLoad里面调用方法如下:
#pragma mark 初始化tabBar对应的ViewControllers
- (void)setControllers{
//配置的各个tabbar对应的controller
self.vc1 = [[UIViewController alloc] init];
self.vc1.view.backgroundColor = [UIColor redColor];
self.vc1.tabBarItem = [self itemWithSelectedImage:@"tabBar_me_icon@2x" image:@"tabBar_me_icon@2x" title:@"首页"];
//配置的各个tabbar对应的controller
self.vc2 = [[UIViewController alloc] init];
self.vc2.view.backgroundColor = [UIColor blueColor];
self.vc2.tabBarItem = [self itemWithSelectedImage:@"tabBar_me_icon@2x" image:@"tabBar_me_icon@2x" title:@"消息"];
//配置的各个tabbar对应的controller
self.vc3 = [[UIViewController alloc] init];
self.vc3.view.backgroundColor = [UIColor cyanColor];
self.vc3.tabBarItem = [self itemWithSelectedImage:@"tabBar_new_icon@2x" image:@"tabBar_new_icon@2x" title:@"发现"];
//配置的各个tabbar对应的controller
self.vc4 = [[UIViewController alloc] init];
self.vc4.view.backgroundColor = [UIColor orangeColor];
self.vc4.tabBarItem = [self itemWithSelectedImage:@"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 = [UIColor greenColor];
self.tabBar.barTintColor = [UIColor whiteColor];
}
- (UITabBarItem *)itemWithSelectedImage:(NSString *)selectImage image:(NSString *)image title:(NSString *)title{
UIImage *im = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:im selectedImage:im];
return item;
}
然后自定义一个继承自UITabBar的类,在该类中设置UITabBar的子控件,在该类中定义一个UIButton属性,放在中间,系统自带的UIBarButton在两侧,将自定义的UIButton设置成懒加载代码如下:
//设置自己定义的button为懒加载并且设置上背景图片
- (UIButton *)btn{
if (_btn==nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
[_btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[_btn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
//button的大小与图片一致
[_btn sizeToFit];
[self addSubview:self.btn];
}
return _btn;
}
在layoutSubviews中设置子控件的布局具体代码如下:
//设置tabbar子控件的布局
- (void)layoutSubviews{
CGFloat w = self.bounds.size.width;
CGFloat h = self.bounds.size.height;
CGFloat btnx = 0;
CGFloat btny = 0;
//5.0是tabbar中的控件的数量
CGFloat width = self.bounds.size.width/5.0;
CGFloat height = self.bounds.size.height;
int i=0;
for (UIView *btn in self.subviews) {
//判断是否是系统自带的UITabBarButton类型的控件
if ([btn isKindOfClass: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,代码如下:
<pre name="code" class="objc">- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
WWTTabBar *tabBar = [[WWTTabBar alloc] initWithFrame:self.tabBar.frame];
tabBar.backgroundColor = [UIColor whiteColor];
//设置tabbar时只能用keyValue方式
[self setValue:tabBar forKeyPath:@"tabBar"];
[self setControllers];
}
最终的截面如下: