标签控制器基本概念
UITabBarController 和 UINavigationController 一样是用来管理视图控制器的.
UINavigationController 是用来管理视图控制器之间的导航, UITabBarController是管理固定的几个视图控制器, 子控制器是并列的. 可以任意切换显示.
很多应用程序都是使用UITabBarController来做整体的布局
UITabBarController 初始化
HomeViewController *home = [[HomeViewControlleralloc] init];
MessageViewController *msg = [[MessageViewControlleralloc] init];
SearchViewController *search = [[SearchViewControlleralloc] init];
SettingViewController *set = [[SettingViewControlleralloc] init];
NSMutableArray *viewControllers = [NSMutableArrayarrayWithObjects:home,msg,search,set, nil];
UITabBarController *tabCtrl = [[UITabBarControlleralloc] init];
//将所有的子控制器交给标签控制器管理
tabCtrl.viewControllers = viewControllers;
self.window.rootViewController = tabCtrl;
设置标题
/*
1.如果当前控制器给导航控制器管理,则此title作为导航栏上的标题显示
2.如果当前控制器给标签控制器管理,则此title作为选项栏上的标题显示
*/
self.title = @"首页";
标签视图控制器的结构
◊ 一个分栏视图控制器控制着若干视图控制器, 他是由他的一个数组进行管理的
◊ 每一个分栏控制器只有一个 UITabBar 视图, 用于显示UITabBarItem实例
◊ UITabBarItem由当前的视图控制器管理, 这一点与导航控制器中的UIBarButtonItem是相同的
UITabBarControl的常用方法:
◊ 创建UITabBarItem
// 创建方式一: 使用系统样式
UITabBarItem *tabItem1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
// 设置一个小图标显示在item上
tabItem1.badgeValue = @"New";
home.tabBarItem = tabItem1;
// 创建方式二: 使用自定义图片, 标题
UIImage *image3 = [UIImageimageNamed:@"tabbar_discover_highlighted.png"];
UITabBarItem *tabItem3 = [[UITabBarItemalloc] initWithTitle:@"搜索"image:image3 tag:3];
search.tabBarItem = tabItem3;
UITabBar工具栏的设置
UITabBarController *tabbarCtrl = [[UITabBarController alloc] init];
UITabBar *tabbar = tabbarCtrl.tabBar;
// 设置背景 (带Bar的不能进行背景颜色设置)
// 背景颜色
tabbar.tintColor = [UIColor grayColor];
// 背景图片
tabbar.backgroundImage = [UIImage imageNamed:@"navbg"];
// 设置选中的Item图标的颜色
tabbar.selectedImageTintColor = [UIColor blueColor];
// 设置选中item后, 显示在item底部的图片
tabbar.selectedIndicatorImage = [UIImage imageNamed:@"选中.jpg"];
用户定制UITabBar
◊ 隐藏自带的UITabBar工具栏
◊ 创建自定义的UITabBar工具栏
具体实现:
self.tabBar.hidden = YES; // 隐藏
// 创建自定义的UITabBar背景视图
UIView *tabbarView = [[UIViewalloc] initWithFrame:CGRectMake(0, 480-49, 320, 49)];
tabbarView.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"navbg.png"]];
[self.viewaddSubview:tabbarView];
[tabbarView release];
// 创建5个按钮,作为UITabBar工具栏上的item
NSArray *array = @[@"1.png",@"2.png",@"3.png",@"4.png",@"5.png"];
for (int i=0; i<array.count; i++) {
NSString *imageName = [array objectAtIndex:i];
UIImage *image = [UIImageimageNamed:imageName];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
float w = 320/5.0;
button.frame = CGRectMake((w-42)/2+w*i, 2, 42, 40);
button.tag = i;
[button addTarget:selfaction:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[tabbarView addSubview:button];
}
//创建选中的图片视图
float w = 320/5.0;
selectedImage = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"选中.png"]];
selectedImage.frame = CGRectMake((w-53)/2, 2, 53, 45);
[tabbarView addSubview:selectedImage];
}
- (void)buttonAction:(UIButton *)button {
int tag = button.tag;
//通过修改selectedIndex,实现切换子控制器的显示
self.selectedIndex = tag;
//错误
// selectedImage.frame.origin.x = 100;
//选中视图的移动效果动画
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:.3];
CGRect frame = selectedImage.frame;
float w = 320/5.0;
frame.origin.x = (w-53)/2+ w * tag;
selectedImage.frame = frame;
[UIViewcommitAnimations];
}
当切换到子视图控制器的导航控制子视图时,隐藏UITabBar的实现
◊ 在UITabBarController控制器类中创建方法:
- (void)showTabbar:(BOOL)show {
CGRect frame = _tabbarView.frame;
if (show == NO) {
frame.origin.x = -320;
} else {
frame.origin.x = 0;
}
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.35];
_tabbarView.frame = frame;
[UIViewcommitAnimations];
}
◊ 在导航子视图中实现
- (void)buttonAction {
UIViewController *detail = [[UIViewControlleralloc] init];
detail.view.backgroundColor = [UIColororangeColor];
detail.title = @"详情页面";
[self.navigationControllerpushViewController:detail animated:YES];
[detail release];
//隐藏tabbar工具栏
MainViewController *mainCtrl = (MainViewController *)self.tabBarController;
[mainCtrl showTabbar:NO];
}
- (void)viewWillAppear:(BOOL)animated {
[superviewWillAppear:animated];
//显示tabbar工具栏
MainViewController *mainCtrl = (MainViewController *)self.tabBarController;
[mainCtrl showTabbar:YES];
}
self.hidesBottomBarWhenPushed = YES; 当切换到导航子视图中获取全部背景视图,隐藏UITabBar