先展示效果图:
这个tabBar纠结了我好久,最后通过查询资料各种百度谷歌终于找到一个比较符合我们需求的案例,下面展示出代码供各位参考
- @interface BaseTabBarViewController ()<UITabBarControllerDelegate>
- @property (nonatomic,strong)UIButton *button;
- @end
- @implementation BaseTabBarViewController
- @synthesize button;
- #pragma mark- setup
- -(void)setup
- {
- // 添加突出按钮
- [self addCenterButtonWithImage:[UIImage imageNamed:@"我的钱"] selectedImage:[UIImage imageNamed:@"我的钱"]];
- // UITabBarControllerDelegate 指定为自己
- self.delegate=self;
- // 指定当前页——中间页
- //self.selectedIndex=0;
- // 设点button状态
- //button.selected=YES;
- // 设定其他item点击选中颜色
- }
- #pragma mark - addCenterButton
- // Create a custom UIButton and add it to the center of our tab bar
- -(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;
- button.center = center;
- [self.view addSubview:button];
- }
- -(void)pressChange:(id)sender
- {
- self.selectedIndex=1;
- button.selected=YES;
- }
- #pragma mark- TabBar Delegate
- // 换页和button的状态关联上
- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
- {
- if (self.selectedIndex==1) {
- button.selected=YES;
- }else
- {
- button.selected=NO;
- }
- }
- @interface BaseTabBarViewController ()<UITabBarControllerDelegate>
- @property (nonatomic,strong)UIButton *button;
- @end
- @implementation BaseTabBarViewController
- @synthesize button;
- #pragma mark- setup
- -(void)setup
- {
- // 添加突出按钮
- [self addCenterButtonWithImage:[UIImage imageNamed:@"我的钱"] selectedImage:[UIImage imageNamed:@"我的钱"]];
- // UITabBarControllerDelegate 指定为自己
- self.delegate=self;
- // 指定当前页——中间页
- //self.selectedIndex=0;
- // 设点button状态
- //button.selected=YES;
- // 设定其他item点击选中颜色
- }
- #pragma mark - addCenterButton
- // Create a custom UIButton and add it to the center of our tab bar
- -(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;
- button.center = center;
- [self.view addSubview:button];
- }
- -(void)pressChange:(id)sender
- {
- self.selectedIndex=1;
- button.selected=YES;
- }
- #pragma mark- TabBar Delegate
- // 换页和button的状态关联上
- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
- {
- if (self.selectedIndex==1) {
- button.selected=YES;
- }else
- {
- button.selected=NO;
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self setTabBarVC];
- [self setup];
- [self addButtonNotifation];
- self.tabBar.barTintColor = [UIColor whiteColor];
- }
- //添加大圆按钮的通知
- -(void)addButtonNotifation{
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonHidden) name:@"buttonNotifationCenter" object:nil];
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonNotHidden) name:@"buttonNotHidden" object:nil];
- }
- -(void)buttonNotHidden{
- button.hidden=NO;
- }
- -(void)buttonHidden{
- button.hidden=YES;
- }
- // 初始化所有子控制器
- - (void)setTabBarVC{
- [self setTabBarChildController:[[SelectionViewController alloc] init] title:@"每月送" image:@"每月送" selectImage:@"每月送选中"];
- [self setTabBarChildController:[[MyMoneyViewController alloc] init] title:@"我的钱" image:@"" selectImage:@""];
- [self setTabBarChildController:[[MainViewController alloc] init] title:@"个人中心" image:@"个人中心" selectImage:@"个人中心选中"];
- }
- // 添加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:10],NSForegroundColorAttributeName:RGBA(74, 74, 74, 1.0)} forState:UIControlStateNormal];
- [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:RGBA(255, 200, 0, 1.0)} forState:UIControlStateSelected];
- [self addChildViewController:nav];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self setTabBarVC];
- [self setup];
- [self addButtonNotifation];
- self.tabBar.barTintColor = [UIColor whiteColor];
- }
- //添加大圆按钮的通知
- -(void)addButtonNotifation{
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonHidden) name:@"buttonNotifationCenter" object:nil];
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonNotHidden) name:@"buttonNotHidden" object:nil];
- }
- -(void)buttonNotHidden{
- button.hidden=NO;
- }
- -(void)buttonHidden{
- button.hidden=YES;
- }
- // 初始化所有子控制器
- - (void)setTabBarVC{
- [self setTabBarChildController:[[SelectionViewController alloc] init] title:@"每月送" image:@"每月送" selectImage:@"每月送选中"];
- [self setTabBarChildController:[[MyMoneyViewController alloc] init] title:@"我的钱" image:@"" selectImage:@""];
- [self setTabBarChildController:[[MainViewController alloc] init] title:@"个人中心" image:@"个人中心" selectImage:@"个人中心选中"];
- }
- // 添加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:10],NSForegroundColorAttributeName:RGBA(74, 74, 74, 1.0)} forState:UIControlStateNormal];
- [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:RGBA(255, 200, 0, 1.0)} forState:UIControlStateSelected];
- [self addChildViewController:nav];
- }
tabBar的颜色各位可以用一张白色的图片做背景
关于那个隐藏大圆按钮的通知,我发现导航推进下一页的时候那个大圆按钮不会消失,我就只好用了一个通知控制隐藏和显示如果各位有更好的方法也希望能够告知,共同进步