问题描述
解决方案
自己自定义一个标签视图控制器吧:
自定义一个类继承自UITabBarController;
然后在该类的.m文件中自己写button和其点击事件间的切换
#import "MyTabBarController.h"
#define WIDTH (myView.frame.size.width / 4)
#define HEIGHT (myView.frame.size.height)
@interface MyTabBarController ()
//之前选中的按钮
@property(nonatomic, retain) UIButton *selectedBtn;
@end
@implementation MyTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
//删除现有的tabBar
CGRect rect = self.tabBar.frame;
[self.tabBar removeFromSuperview];//添加自己的视图
UIView *myView = [[BaseView alloc] init];
myView.backgroundColor = [UIColor whiteColor];
myView.frame = rect;
[self.view addSubview:myView];//主页发现按钮
UIButton *findBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[findBtn setImage:[UIImage imageNamed:@"icon_home_n"] forState:UIControlStateNormal];
[findBtn setImage:[UIImage imageNamed:@"icon_home_h"] forState:UIControlStateSelected];
findBtn.tag = 1;
findBtn.frame = CGRectMake(0, -20, WIDTH, HEIGHT); //y设成负值,高再设高一点就好了!!!
//默认选中是发现按钮
findBtn.selected = YES;
[findBtn setTintColor:[UIColor colorWithRed:0.886 green:0.365 blue:0.247 alpha:1.000]];
self.selectedBtn = findBtn;
[myView addSubview:findBtn];UILabel *findLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
findLabel.text = @"发现";
findLabel.textColor = [UIColor colorWithRed:0.804 green:0.325 blue:0.227 alpha:1.000];
findLabel.font = [UIFont systemFontOfSize:10];
findLabel.tag = 101;
[myView addSubview:findLabel];
[findLabel release];//搜索页
UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[searchBtn setImage:[UIImage imageNamed:@"icon_search_n"] forState:UIControlStateNormal];
[searchBtn setImage:[UIImage imageNamed:@"icon_search_h"] forState:UIControlStateSelected];
searchBtn.tag = 2;
searchBtn.frame = CGRectMake(WIDTH, -20, WIDTH, HEIGHT);
[myView addSubview:searchBtn];UILabel *searchLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
searchLabel.text = @"搜索";
searchLabel.textColor = [UIColor grayColor];
searchLabel.font = [UIFont systemFontOfSize:10];
searchLabel.tag = 102;
[myView addSubview:searchLabel];
[searchLabel release];//收藏页
UIButton *costBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[costBtn setImage:[UIImage imageNamed:@"iconfont-like"] forState:UIControlStateNormal];
[costBtn setImage:[UIImage imageNamed:@"iconfont-like-selected"] forState:UIControlStateSelected];
costBtn.tag = 3;
costBtn.frame = CGRectMake(2 * WIDTH, -20, WIDTH, HEIGHT);
[myView addSubview:costBtn];UILabel *collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
collectLabel.text = @"收藏";
collectLabel.textColor = [UIColor grayColor];
collectLabel.font = [UIFont systemFontOfSize:10];
collectLabel.tag = 103;
[myView addSubview:collectLabel];
[collectLabel release];//设置页
UIButton *setUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[setUpBtn setImage:[UIImage imageNamed:@"icon_setting_n"] forState:UIControlStateNormal];
[setUpBtn setImage:[UIImage imageNamed:@"icon_setting_h"] forState:UIControlStateSelected];
setUpBtn.tag = 4;
setUpBtn.frame = CGRectMake(3 * WIDTH, -20, WIDTH, HEIGHT);
[myView addSubview:setUpBtn];UILabel *setUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(3 * WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
setUpLabel.text = @"设置";
setUpLabel.textColor = [UIColor grayColor];
setUpLabel.font = [UIFont systemFontOfSize:10];
setUpLabel.tag = 104;
[myView addSubview:setUpLabel];
[setUpLabel release];for (int i = 1; i <4; i++) {
UIButton *btn = [self.view viewWithTag:i];
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}
}
#pragma mark -- 自定义tabBar按钮的点击事件
-(void)clickBtn:(UIButton *)sender {
//设置上一个按钮
self.selectedBtn.selected = NO;
UILabel *titleFormer = [self.view viewWithTag:self.selectedBtn.tag + 100];
titleFormer.textColor = [UIColor grayColor];
//设置选中状态
sender.selected = YES;
self.selectedBtn = sender;
UILabel *titleNow = [self.view viewWithTag:self.selectedBtn.tag + 100];
titleNow.textColor = [UIColor colorWithRed:0.804 green:0.325 blue:0.227 alpha:1.000];
//跳转相应的视图控制器
self.selectedIndex = sender.tag - 1;
}
解决方案二:
自己放ImageView等,把图片贴过去
解决方案三:
点击的时候将图片的frame变大不就行了
解决方案四:
自己在 tabbar 上添加一个 比 tabbar 稍高一点的view, 在 view 上添加四个按钮,把每个按钮的图片设置成图中需要的小猴子图片,点击按钮的时候去通知控制器改变 tabbar