UITabBarController详解(三)自定义UITabBarController
最后更新于:2022-04-01 06:59:28
@有的时候,我们不想用系统提供的UITabBarController,这个时候就需要自定义了,还能实现一定的动画效果,比如仿新浪的,切换标签,会有图片随着移动
@自定义的时候,一般是用UIImageView+UIButton实现
~~~
#pragma mark - 自定义TarBar
- (void)createCustomTarBar{
#pragma mark 初始化TarBar背景
UIImageView * tarbarBackground = [[UIImageView alloc]initWithFrame:CGRectMake(0, ScreenHeight-49, ScreenWidth, 49)];
tarbarBackground.image = [UIImage imageNamed:@"tarBarBackground"];
// 切记,不能漏了这句话,UIImageView会阻挡程序检测到点击在button上
tarbarBackground.userInteractionEnabled = YES;
[self.view addSubview:tarbarBackground];
[tarbarBackground release];
#pragma mark 初始化选中时的图片
_selectImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"select"]];
_selectImageView.frame = CGRectMake(5, 4, 54, 42);
// 思考为什么,这里不需要开交互,点击button也能响应
//selectImageView.userInteractionEnabled = NO;
[tarbarBackground addSubview:_selectImageView];
[_selectImageView release];
#pragma mark 初始化tabBarItem----->UIButton
NSArray * defaultBackground = @[@"tabbar_home",@"tabbar_message_center",@"tabbar_profile",@"tabbar_discover",@"tabbar_more"];
for (int i = 0; i < 5; i++) {
// button与tarbarBackground 是上下的层级关系
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10+64*i, 5, 44, 41);
button.tag = TAG + i;
[button setImage:[UIImage imageNamed:[defaultBackground objectAtIndex:i]] forState:UIControlStateNormal];
[button addTarget:self action:@selector(onClickChangeView:) forControlEvents:UIControlEventTouchUpInside];
[tarbarBackground addSubview:button];
}
}
- (void)onClickChangeView:(UIButton *)button{
self.selectedIndex = button.tag;
// block设定动画
[UIView animateWithDuration:0.5 animations:^{
_selectImageView.frame = CGRectMake(5 + button.tag * 64, 4, 54, 42);
}];
}
~~~
~~~
#pragma mark - 初始化ViewController
- (void)createTarBarViewController{
#pragma mark 第一个视图ViewController
HMT_AViewController * tabBarViewA = [[HMT_AViewController alloc] init];
// 设置A视图下----标签栏标题文字(可参照微信或者QQ体会)
//tabBarViewA.tabBarItem.title = @"微信";
// 设置A视图下----标签栏图片(因为自己没有图片,在这里随便设置了个名字)
//tabBarViewA.tabBarItem.image = [UIImage imageNamed:@"1.png"];
// 设置A视图下----标签栏信息提示(住:badgeValue是NSString类型 如下设置了3,就像QQ消息有3条未接受一样,给人一种提醒)
//tabBarViewA.tabBarItem.badgeValue = @"3";
UINavigationController * tabBarViewNA = [[UINavigationController alloc]initWithRootViewController:tabBarViewA];
// ios7弃用了----标签栏选中的时候显示一张图片,没选中的时候显示另一张图片
//[tabBarViewA.tabBarItem setFinishedSelectedImage:actionMenu.selectedIcon withFinishedUnselectedImage:actionMenu.icon];
// ios7的方法(自己没有图片,所以代码里面的图片都是一个随便取的名字,没有实用意义)
//tabBarViewA.tabBarItem.selectedImage = actionMenu.selectedIcon;
#pragma mark 第二个视图ViewController
// 第二个视图ViewController
HMT_BViewController * tabBarViewB = [[HMT_BViewController alloc] init];
// 设置B视图下----标签栏
// 用系统提供的标识(可以算等价于图标和文字)进行设置(参数:UITabBarSystemItem是个枚举值,想要什么形式,就去系统提供的API中找)
//tabBarViewB.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1]autorelease];
// 设置B视图下----标签栏信息提示
//tabBarViewB.tabBarItem.badgeValue = @"GO";
#pragma mark 第三个视图ViewController
HMT_CViewController * tabBarViewC = [[HMT_CViewController alloc] init];
// ViewController -- NavigationController -- UITabBarController 3者想相结合
UINavigationController * tabBarViewNC = [[UINavigationController alloc]initWithRootViewController:tabBarViewC];
tabBarViewC.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:2]autorelease];
// 设置B视图下----标签栏信息提示
//tabBarViewC.tabBarItem.badgeValue = @"new";
#pragma mark 第四个视图ViewController
HMT_DViewController * tabBarViewD = [[HMT_DViewController alloc] init];
//tabBarViewD.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3]autorelease];
// 设置B视图下----标签栏信息提示
//tabBarViewD.tabBarItem.badgeValue = @"99";
#pragma mark 第五个视图ViewController
HMT_EViewController * tabBarViewE = [[HMT_EViewController alloc] init];
//tabBarViewE.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:4]autorelease];
// 设置B视图下----标签栏信息提示
//tabBarViewE.tabBarItem.badgeValue = @"sky";
#pragma mark 第六个视图ViewController(系统默认能显示的最大视图个数是5个)
/* 如果你的viewControllers属性添加了多于五个的items,那tab bar controller将会自动插入一个特殊的view controller,
称为 More view controller,该 controller 将会负责管理多于的items,这个More view controller提供一个自定义的界面,
用table的方式呈现多余的view controller,并且view controller的数量是不限制的*/
HMT_FViewController * tabBarViewF = [[HMT_FViewController alloc] init];
//tabBarViewF.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:5]autorelease];
// 设置F视图下----标签栏信息提示
//tabBarViewF.tabBarItem.badgeValue = @"AG";
#pragma mark 设置TabBarController
// 创建TabBarController
//UITabBarController * tabBarController = [[UITabBarController alloc]init];
// TabBarController默认是放在最底部的,如果你想调整位置,可以进行下面2部操作(44是iPhone中TabBarController和UINavigationController标准高度)
//CGRect frame = CGRectMake(0, 20, 320, 44);
//tabBarController.tabBar.frame = frame;
// 每一个tab都必须有一个content view controller------->viewControllers属性,用来存入一个应用的TabBarController有多少个界面切换
self.viewControllers = [NSArray arrayWithObjects:tabBarViewNA,tabBarViewB,tabBarViewNC,tabBarViewD,tabBarViewE,nil];
// 设置着色
self.tabBar.tintColor = [UIColor greenColor];
// 设置选中图片时候
self.tabBar.selectedImageTintColor = [UIColor redColor];
// 设置背景图片(自己没有图片,不进行设置)
//tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"@@@@@"];
// 设置程序启动时默认的ViewController视图(设置为3,一共5个ViewController,进来时候显示的视图就是第4个-tabBarViewD,下标从0开始)
//self.selectedIndex = 0;
// 设置代理
self.delegate = self;
//UINavigationController *nav = tabBarController.moreNavigationController;
//[nav setNavigationBarHidden:YES animated:YES];
[tabBarViewA release];
[tabBarViewNA release];
[tabBarViewB release];
[tabBarViewC release];
[tabBarViewNC release];
[tabBarViewD release];
[tabBarViewE release];
[tabBarViewF release];
}
// 控制哪些ViewController的标签栏能被点击
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
// 代表HMT_CViewController这个View无法显示,无法点击到它代表的标签栏
/*if ([viewController isKindOfClass:[HMT_CViewController class]]) {
return NO;
}*/
return YES;
}
// 选中哪个标签栏,一个监控作用吧
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLOG_FUNCTION;
}
// More view controller 将要开始编辑
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers{
NSLOG_FUNCTION;
}
// More view controller 将要结束编辑
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
NSLOG_FUNCTION;
}
// More view controller 编辑
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
NSLOG_FUNCTION;
}
~~~
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-12_5694d753c8211.jpg)