Tab控制器 ( UITabBarController )
来源:互联网 发布:知乎炸鱼 编辑:程序博客网 时间:2024/04/20 14:11
标签控制器基本概念
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:imageforState: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:detailanimated: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
- Tab控制器 ( UITabBarController )
- 控制器:UITabBarController
- UITabBarController实现Tab切换
- UITabBarController 标签栏控制器
- UITabBarController标签栏控制器
- ios 控制器 UITabBarController
- UITabBarController 标签栏控制器
- UITabBarController标签视图控制器
- UITabBarController ---- 标签视图控制器
- 19-UITabBarController 标签控制器
- UITabBarController--标签控制器
- 分栏控制器UITabBarController
- UITabBarController- 标签视图控制器
- UITabBarController 根视图控制器
- 多控制器-UITabBarController
- Tab控制器
- UITabBarController 隐藏 Bootom Tab Bar
- UITabBarController — 标签视图控制器
- 统计学习方法第四章,贝叶斯估计的实现
- 如何评估个人的软件开发能力
- 如何彻底的删除Oracle表
- poj 1195 二维树状数组
- 谁在使用我的网站——用户忠诚和价值分析
- Tab控制器 ( UITabBarController )
- 数据库相关
- JDK环境变量设置详解
- Eclipse中调用VS 2012生成的DLL文件,进行调试的步骤。。。[含VS2012中release版本的调试配置]
- Linux2.6内核--内存管理(1)--分页机制
- shell字符串操作之cut---实现字符串截取
- cocos2d-x锚点问题
- android 编译系统文件结构
- http协议详解