效果:
<img src="https://img-blog.csdn.net/20151112124907132?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
.h文件
#import <UIKit/UIKit.h>
#import "NowTableViewController.h"
#import "HappyOTableViewController.h"
#import "MarketTableViewController.h"
//继承自 UITabBarController(注意!)
@interface ShoppingViewController : UITabBarController <UIApplicationDelegate>
{
NowTableViewController *listViewControllerNow;
HappyOTableViewController *listViewControllerHappy;
MarketTableViewController *listViewControllerMarket;
UITableViewController *currentViewController;
@private UIImageView *_selectView;
}
@end
.m文件
//添加导航控制器到视图上
-(void)loadViewControllers
{
listViewControllerNow = [[NowTableViewController alloc] initWithStyle:UITableViewStylePlain];
listViewControllerHappy = [[HappyOTableViewController alloc] init];
listViewControllerMarket = [[MarketTableViewController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:listViewControllerNow,listViewControllerHappy, listViewControllerMarket, nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:viewControllers];
[self setViewControllers:viewControllers animated:YES];
}
/**
* 加载tabBar
*/
-(void)loadCustomTabBar
{
/**1、
初始化自定义TabBar背景
*/
UIImageView *tabBarBG=[[UIImageView alloc]initWithFrame:CGRectMake(0,SCREENHEIGHT/3-SCREENHEIGHT/3, SCREENWIDTH,SCREENHEIGHT/15)];
tabBarBG.userInteractionEnabled=YES;
tabBarBG.backgroundColor=[UIColor blackColor];
[self.view addSubview:tabBarBG];
/**3、
初始化自定义按钮选中背景
*/
_selectView=[[UIImageView alloc]initWithFrame:CGRectMake(2, 4, SCREENWIDTH/3,SCREENHEIGHT/15-8)];
_selectView.image=[UIImage imageNamed:@"滑动图片"];
[[_selectView layer] setCornerRadius:20.0];
[tabBarBG addSubview:_selectView];
/**2、
* 初始化自定义TabBarItem—>UIButton
*/
float coordinateX=0;
for (int index=0; index<3;index++)
{
//初始化按钮位置
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=index;
button.frame=CGRectMake(0+coordinateX, 4, SCREENWIDTH/3, SCREENHEIGHT/15-8);
// [button.layer setCornerRadius:8.0];
//为按钮填加名字
NSString *Index=[NSString stringWithFormat:@"%d",index]; //当前按钮的索引
NSDictionary *buttonName=[NSDictionary dictionaryWithObjectsAndKeys:@"正在进行",@"0",@"乐享聚点",@"1",@"购物商城",@"2", nil];
[button setTitle:[buttonName objectForKey:Index] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:14]];
//为按钮填加背景图片
NSString *imageName=[NSString stringWithFormat:@"%d",index+1];
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[tabBarBG addSubview:button];
//为按钮填加转换视图控制器动作
[button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
coordinateX+=SCREENWIDTH/3;
}
}
/**
* 根据按钮的点击事件跳转视图控制器
*
* @param button 跳转的按钮
*/
-(void)changeViewController:(UIButton *)button
{
//获取点击按钮的tag
self.selectedIndex=button.tag;
//为点击事件添加动画
[UIView beginAnimations:nil context:NULL];
_selectView.frame=CGRectMake(SCREENWIDTH/3*button.tag, 4, SCREENWIDTH/3,SCREENHEIGHT/15-8);
[[_selectView layer] setCornerRadius:10.0];
NSLog(@"跳转到%ld",(long)button.tag);
[UIView commitAnimations];
}