标签:
思路:1.定义一个BaseTabBarViewController类继承自UITabBarController
2.将原先的tabbar隐藏,自定义一个控件覆盖在上面
3.在控件内增加可以点击的按钮,调整控件和按钮的具体细节
具体代码如下:
.h里
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "HZSButton.h"
@interface BaseTabBarViewController : UITabBarController<UITabBarControllerDelegate>
{
UIImageView *_tabBarView; //自定义的覆盖原先的tarbar的控件
HZSButton *_previousBtn; //记录前一次选中的按钮
}
@end
.m里
#import "BaseTabBarViewController.h"
@interface BaseTabBarViewController ()
@end
@implementation BaseTabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//我要投资
FirstViewController *aFirstViewController=[[FirstViewController alloc]init];
UINavigationController *nav0=[[UINavigationController alloc]initWithRootViewController:aFirstViewController];
//我的账户
SecondViewController *aSecondViewController=[[SecondViewController alloc]init];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:aSecondViewController];
//更多
ThirdViewController *aThirdViewController=[[ThirdViewController alloc]init];
UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:aThirdViewController];
self.viewControllers=[NSArray arrayWithObjects:nav0,nav1,nav2,nil];
self.delegate=self;
self.tabBar.hidden = YES; //隐藏原先的tabBar
CGFloat tabBarViewY = self.view.frame.size.height - 49;
_tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, tabBarViewY, self.view.frame.size.width, 49)];
_tabBarView.userInteractionEnabled = YES; //这一步一定要设置为YES,否则不能和用户交互
// _tabBarView.image = [UIImage imageNamed:@"NavBar_ios7@3x.png"];
_tabBarView.backgroundColor=[UIColor colorWithRed:232.0/255.0 green:235.0/255.0 blue:240.0/255.0 alpha:1];
[self.view addSubview:_tabBarView];
// 下面的方法是调用自定义的生成按钮的方法
[self creatButtonWithNormalName:@"tab_1_unselected@2x.png"andSelectName:@"tab_1_selected@2x.png"andTitle:@"消息"andIndex:0];
[self creatButtonWithNormalName:@"tab_2_unselected@2x.png"andSelectName:@"tab_2_selected@2x.png"andTitle:@"联系人"andIndex:1];
[self creatButtonWithNormalName:@"tab_3_unselected@2x.png"andSelectName:@"tab_3_selected@2x.png"andTitle:@"我的"andIndex:2];
HZSButton *btn = (HZSButton *)[_tabBarView viewWithTag:8888];
[self changeViewController:btn]; //自定义的控件中的按钮被点击了调用的方法,默认进入界面就选中第一个按钮
}
//创建一个按钮
-
(void)creatButtonWithNormalName:(NSString *)normal
andSelectName:(NSString *)selected andTitle:(NSString *)title
andIndex:(int)index
{
/*
HZSButton是自定义的一个继承自UIButton的类,自定义该类的目的是因为系统自带的Button可以设置image和title属性,但是
默认的image是在title的左边,若想想上面图片中那样,将image放在title的上面,就需要自定义Button,设置一些东西。(具体
HZSButton设置了什么,放在下面讲)
*/
CGFloat buttonW = _tabBarView.frame.size.width / 3;
CGFloat buttonH = _tabBarView.frame.size.height;
UIView *aview=[[UIView alloc]init];
aview.backgroundColor=[UIColor colorWithRed:46.0/255.0 green:167.0/255.0 blue:224.0/255.0 alpha:1];
aview.frame = CGRectMake(buttonW *index+buttonW/2.0-17.5, 0, 35, buttonH);
[_tabBarView addSubview:aview];
HZSButton *button = [HZSButton buttonWithType:UIButtonTypeCustom];
button.tag = index+8888;
button.frame = CGRectMake(buttonW *index, 0, buttonW, buttonH);
[button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
[button setImage:[[UIImage imageNamed:selected]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
forState:UIControlStateDisabled];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
button.imageView.contentMode = UIViewContentModeCenter; // 让图片在按钮内居中
button.titleLabel.textAlignment = NSTextAlignmentCenter; // 让标题在按钮内居中
button.titleLabel.font = [UIFont systemFontOfSize:12]; // 设置标题的字体大小
[button setTitleColor:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:242.0/255.0 green:132.0/255.0 blue:44.0/255.0 alpha:1] forState:UIControlStateDisabled];
[_tabBarView addSubview:button];
}
//按钮被点击时调用
- (void)changeViewController:(HZSButton *)sender
{
self.selectedIndex = sender.tag-8888; //切换不同控制器的界面
self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:self.selectedIndex];
sender.enabled = NO;
if (_previousBtn != sender) {
_previousBtn.enabled = YES;
}
_previousBtn = sender;
NSLog(@"self.selectedIndex%ld",self.selectedIndex);
}
iOS快速自定义UITabBarController内的tabbar控件
标签:
原文地址:http://www.cnblogs.com/huangzs/p/4527379.html