关于UITabBar各部分自定义的代码片段(转载)
2012-02-21 14:19
344 查看
转载自:http://www.cnblogs.com/lovecode/articles/2310977.html
一、自定义TabBar选项卡背景
默认UITabBarController的TabBar背景是黑色的,如何自定义成背景图片呢?
复制代码
或者:
复制代码
或者:
复制代码
或者:在UITabBarController子类中重写init方法来初始化
复制代码
当然在iOS5开始就最方便了,在iOS5中提供了一个API来设置UITabBar的背景图片,以及表示选中的图片。
复制代码
二、隐藏TabBar
隐藏系统TabBar,但仍占位置。
复制代码
彻底隐藏TabBar:如从一个有TabBar控制器的视图转入另外一新视图,且没有上一视图的TabBar。
复制代码
一、自定义TabBar选项卡背景
默认UITabBarController的TabBar背景是黑色的,如何自定义成背景图片呢?
UITabBarController *tabBarController = [[UITabBarController alloc] init]; // 获取选项卡控制器视图的所有子视图,保存到一数组中 NSArray *array = [tabBarController.view subviews]; // 索引值为1的应该就是TabBar UITabBar *tabBar = [array objectAtIndex:1]; // UIImage *image = [UIImage imageNamed:@"tabbarbg.png"]; UIImage *image = [UIImage imageWithContentsOfFile:sourcePath]; tabBar.layer.contents = (id)image.CGImage;
复制代码
或者:
tabBarController = [[UITabBarController alloc] init]; [tabBarController setViewControllers: view_manager]; UIImageView *tab_imgv = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_bg.png"]]; tab_imgv.frame = CGRectMake(0,0,320,49); tab_imgv.contentMode = UIViewContentModeScaleToFill; // 为什么atIndex是1,看SDK [[tabBarController tabBar] insertSubview:tab_imgv atIndex:1]; [tab_imgv release]; [view_manager release];
复制代码
或者:
UITabBarController *tabBarController = [[UITabBarController alloc] init]; // 初始化一矩形视图框架 CGRect frame = CGRectMake(0,0,320,49); UIView *v = [[UIView alloc] initWithFrame:frame]; // 以图片为平铺的颜色模板,初始化颜色 UIImage *img = [UIImage imageNamed:@"tabbarbg.png"]; UIColor *color = [[UIColor alloc] initWithPatternImage:img]; // 设置视图背景色 v.backgroundColor = color; // 将视图插入到选项卡栏底层 [tabBarController.tabBar insertSubview:v atIndex:0]; tabBarController.tabBar.opaque = YES; [color release]; [v release];
复制代码
或者:在UITabBarController子类中重写init方法来初始化
- (id)init { if(self=[super init]){ //方法一 UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbarbg.png"]]; imgv.frame = CGRectMake(0,0,self.tabBar.frame.size.width,self.tabBar.frame.size.height); imgv.contentMode = UIViewContentModeScaleToFill; // imgv.frame = CGRectOffset(imgv.frame,0,1); [[self tabBar] insertSubview:imgv atIndex:0]; [imgv release]; // 方法二 CGRect frame = CGRectMake(0,0,self.view.bounds.size.width,49); UIView *view = [[UIView alloc] initWithFrame:frame]; UIImage *tabImage = [UIImage imageNamed:@"tabbg.png"]; UIColor *color = [[UIColor alloc] initWithPatternImage:tabImage]; [view setBackgroundColor:color]; [color release]; [[self tabBar] insertSubview:view atIndex:0]; [view release]; } }
复制代码
当然在iOS5开始就最方便了,在iOS5中提供了一个API来设置UITabBar的背景图片,以及表示选中的图片。
UIImage *tabBackground = [[UIImage imageNamed:@"tab_bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [[UITabBar appearance] setBackgroundImage:tabBackground]; [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_select_indicator.png"]];
复制代码
二、隐藏TabBar
隐藏系统TabBar,但仍占位置。
- (void)hideExsitingTabBar { for(UIView *view in self.view.subviews) { if([view isKindOfClass:[UITabBar class]]) { view.hidden = YES; break; } } }
复制代码
彻底隐藏TabBar:如从一个有TabBar控制器的视图转入另外一新视图,且没有上一视图的TabBar。
nextViewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:nextViewController animated:NO];
复制代码
相关文章推荐
- 关于UITabBar各部分自定义的代码片段(转载)
- 收集几段android代码,关于google map 中overlay部分(转载)
- 《 Swift UITabBarController 的使用和自定义TabBar,和部分属性和代理的使用》
- 关于sep4020 arm sample代码中启动部分,中断返回部分,misc部分的修订
- 关于如何使用自定义的结束消息循环的方式 (转载)
- iOS XCode: 如何添加自定义代码片段
- VS2015使用技巧 打开代码片段C#部分
- Sublime Text3—自定义代码片段(Code Snippets)
- 关于VS2005中GridView的自定义分页,单选、多选、排序、自增列的简单应用(转载的)
- 【转载】C# 读取Excel文件代码的几种片段收集
- 转载,关于在Javascript里自定义对象以及使用案例。
- 一步一步学习sharepoint2010 workflow 系列第二部分:SharePoint无代码工作流 第6章 自定义Visio SharePoint 工作流(custome visio SharePoint workflow)
- 关于跨浏览器的部分代码的封装
- 关于部分自定义静态库(*.lib)在Pocket PC不能使用的问题
- openssl中关于engine的部分代码分析
- VS2015使用技巧 打开代码片段C#部分
- XCode: 如何添加自定义代码片段
- iOS 关于自定义转场动画,以UITabBarController为例
- 关于C中的部分小知识点(都是转载,个人学习,如有侵权,请联系我删除~谢谢!)
- 驱动程序的内存分配(关于锁定驱动代码和数据常驻内存 学习学习红色标记部分)