iOS开发中经常会遇到自定义tabbar,有时候会要求中间的Item往上突起,这里做了个简单的封装,只要在tabbarController中实现即可,先看看效果图:
以下是实现代码,只要把代码拷贝进去,里面的试图控制器替换成自己的即可,自己也可以做相应的修改:
视图控制器继承自tabarControllder
.h文件:
//
// MainViewController.h
// tabbarDemo
//
// Created by HandsomeC on 17/6/19.
// Copyright © 2017年 赵发生. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MainViewController : UITabBarController
@end
.m文件:
//
// MainViewController.m
// tabbarDemo
//
// Created by HandsomeC on 17/6/19.
// Copyright © 2017年 赵发生. All rights reserved.
//
#import "MainViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "SendViewController.h"
#import "KindViewController.h"
#import "MineViewController.h"
#define RGBCOL(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
@interface MainViewController ()<UITabBarControllerDelegate>
@property (nonatomic,strong)UIButton *button;
@property (nonatomic, strong) HomeViewController *homeVC;
@property (nonatomic, strong) KindViewController *kindVC;
@property (nonatomic, strong) MessageViewController *msgVC;
@property (nonatomic, strong) MineViewController *mineVC;
@end
@implementation MainViewController
@synthesize button;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:238/255.0f green:238/255.0f blue:238/255.0f alpha:1.0];
[self setTabBarVC];
// 添加中间突出按钮
[self addCenterButtonWithImage:[UIImage imageNamed:@"send"] selectedImage:[UIImage imageNamed:@"send"]];
//设置代理方法
self.delegate = self;
[self.tabBar setBackgroundImage:[UIImage imageNamed:@"line"]];
}
/**
重写突出按钮的点击事件,使其与tabbarItem中间那个按钮的点击事件一致,点击的时候,其他按钮选中为否,当前tababtrController选中视图为中间按钮的值,self.selectedIndex随着中间按钮的位置而变动
@param sender 点击的按钮
*/
-(void)pressChange:(id)sender
{
self.selectedIndex=2;
button.selected=NO;
}
/**
初始化所有子视图控制器
这里的自视图控制器可以自己任意删除和添加,在中间需要突出的按钮那里不要设置图片,只需要设置视图标题,即为tabbarItem的下方文字
*/
- (void)setTabBarVC{
[self setTabBarChildController:[[HomeViewController alloc] init] title:@"库购" image:@"home" selectImage:@"home1"];
[self setTabBarChildController:[[KindViewController alloc] init] title:@"分类" image:@"kind" selectImage:@"kind1"];
[self setTabBarChildController:[[SendViewController alloc] init] title:@"发布" image:@"" selectImage:@""];
[self setTabBarChildController:[[MessageViewController alloc] init] title:@"消息" image:@"massage" selectImage:@"massage1"];
[self setTabBarChildController:[[MineViewController alloc] init] title:@"我的" image:@"mine" selectImage:@"mine1"];
}
/**
添加中间按钮,这里可以有选中按钮
@param buttonImage 中间突出按钮的正常图片
@param selectedImage 中间突出按钮的选中图片,如果没有,则为空
*/
-(void) addCenterButtonWithImage:(UIImage*)buttonImage selectedImage:(UIImage*)selectedImage
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
//重写中间按钮的点击事件
[button addTarget:self action:@selector(pressChange:) forControlEvents:UIControlEventTouchUpInside];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
// 设定button大小为适应图片
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
// 去掉选中button时候的阴影
button.adjustsImageWhenHighlighted=NO;
/*
* 核心代码:设置button的center 和 tabBar的 center 做对齐操作, 同时做出相对的上浮
*/
CGPoint center = self.tabBar.center;
//这里是上浮操作,把中间按钮往上移动相应像素
center.y = center.y - buttonImage.size.height/4-10;
button.center = center;
[self.view addSubview:button];
}
// 添加tabbar的子viewcontroller
- (void)setTabBarChildController:(UIViewController*)controller title:(NSString*)title image:(NSString*)imageStr selectImage:(NSString*)selectImageStr{
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller];
nav.tabBarItem.title = title;
nav.tabBarItem.image = [[UIImage imageNamed:imageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//设置正常的文字颜色
[nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RGBCOL(74, 74, 74, 1.0)} forState:UIControlStateNormal];
//设置选中的文字颜色
[nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RGBCOL(159, 215, 5, 1.0)} forState:UIControlStateSelected];
[self addChildViewController:nav];
}
//重写代理方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (self.selectedIndex==1) {
button.selected=YES;
}else
{
button.selected=NO;
}
}
@end