iOS中tabBar的基础介绍和用法
2015-07-14 20:05
603 查看
tabBar的定义
2.给window添加根视图控制器
3.自定义badgevalue,并改变位置和大小,值得注意的是,一定要在创建uitabBarcontroller对象后,再定义角标
4.重写初始化方法
5.一点补充
tabBar中一个很重要的属性是hidesBottomBarWhenPushed,该属性控制该视图在push到其他界面时是否会隐藏tabBar,默认置为NO。
//创建第一个导航视图控制器管理的根视图控制器 FirstViewController *_firstVC = [[FirstViewController alloc]init]; //创建第一个导航视图控制器对象,用来管理——firstvc UINavigationController *_firstNC = [[UINavigationController alloc]initWithRootViewController:_firstVC]; //创建第二个导航视图控制器管理的根视图控制器 ScondViewController *_secondVC = [[ScondViewController alloc]init]; //创建第2个导航视图控制器对象,用来管理_secondVC UINavigationController *_secondNC = [[UINavigationController alloc]initWithRootViewController:_secondVC]; //这里只上传了两个,其他的可以类推,本demo中共创建了6个 //创建标签视图控制器对象 UITabBarController *_tabBarController = [[UITabBarController alloc]init]; //给标签视图控制器添加所管理的视图控制器 (导航控制器) _tabBarController.viewControllers = @[_firstNC,_secondNC,_thirdNC,_forthNC,_fifthNC,_sixthNC]; //2.改变tabBar自身的颜色 //_tabBarController.tabBar.barTintColor = [UIColor redColor]; //3.改变tabBar上的视图的渲染颜色 // _tabBarController.tabBar.tintColor = [UIColor redColor]; //4.设置tabBar默人选中item 默认是0 _tabBarController.selectedIndex = 0; //5.给 tabBar添加背景图片 tabBar的高度是49 _tabBarController.tabBar.backgroundImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"123" ofType:@"jpg"]];
2.给window添加根视图控制器
self.window.rootViewController = _tabBarController;
3.自定义badgevalue,并改变位置和大小,值得注意的是,一定要在创建uitabBarcontroller对象后,再定义角标
UILabel *badgeLabel = [[UILabel alloc]initWithFrame:CGRectMake(270, 4, 16, 16)]; badgeLabel.backgroundColor = [UIColor redColor]; badgeLabel.text = @"5"; badgeLabel.textAlignment = NSTextAlignmentCenter; badgeLabel.textColor = [UIColor whiteColor]; badgeLabel.font = [UIFont systemFontOfSize:16]; //截取圆角 badgeLabel.layer.cornerRadius = 8;//圆角弯曲程度 badgeLabel.layer.masksToBounds = YES;//是否减掉边角 //放在tabBar上显示 [_tabBarController.tabBar addSubview:badgeLabel]; [badgeLabel release];
4.重写初始化方法
//重写初始化方法 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // //创建firstvc里的标签视图(使用系统自带方法) // UITabBarItem *firstItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:101]; // //给tabBar上添加标签视图 // self.tabBarItem = firstItem; // // //给tabBar上的视图添加角标 //// firstItem.badgeValue = @"10"; // [firstItem release]; //自定义标签视图 UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:nil image:[[UIImage imageNamed:@"icon01"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"icon01_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];//,该方法中第一个参数为tabBar上图片下的文字, 第2个参数为未点击时图片样式,第3个参数为点击时的图片样式 // 添加标签视图 self.tabBarItem = tabBarItem; [tabBarItem release]; //调整图标的位置 使用uitabbaritem父类中的继承到的属性 imagesInset(上,左,下,右) tabBarItem.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0); } return self; }
5.一点补充
tabBar中一个很重要的属性是hidesBottomBarWhenPushed,该属性控制该视图在push到其他界面时是否会隐藏tabBar,默认置为NO。
@property(nonatomic) BOOL hidesBottomBarWhenPushed; // If YES, then when this view controller is pushed into a controller hierarchy with a bottom bar (like a tab bar), the bottom bar will slide out. Default is NO.
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- ip地址基础知识
- VBS基础编程教程 (第1篇)
- VBS基础编程教程 (第3篇)
- 路由器基础精析
- VBS基础编程教程 (第4篇)
- VBS基础编程教程 (第5篇)
- VBS基础编程教程 (第6篇)
- VBS编程教程 (第2篇)
- AJAX初级教程之初识AJAX
- Jquery 基础学习笔记
- PHP学习一(基础)第1/2页
- 《JavaScript DOM 编程艺术》读书笔记之DOM基础
- 精通Javascript系列之Javascript基础篇