-
( void )viewWillAppear:( BOOL )animated
|
03
|
[super
viewWillAppear:animated];
|
05
|
for (UIView
*view in self.tabBarController.tabBar.subviews)
|
07
|
if ([view
isKindOfClass:[UIImageView class ]])
|
09
|
[view
removeFromSuperview];
|
13
|
[self.tabBarController.tabBar
insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@ "background_location.png" ]]
autorelease] atIndex:0];
|
转自http://www.cnblogs.com/lovecode/articles/2310977.html
一、自定义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];
分享到:
相关推荐
uniapp小程序tabbar整套图标,包含选中和非选择的
微信小程序tabbar图标素材
微信小程序tabbar图标素材
uniapp自定义tabbar图标样式uniapp自定义tabbar图标样式
微信小程序学习用demo:LOOK:点击tabbar变换图标
IOS应用源码之100多个tabBar icons图标资源.rar
点击tabbar变换图标.rar
小程序tabbar素材小程序tabbar素材小程序tabbar素材小程序tabbar素材 微信小程序tabbar小图片
点击tabbar变换图标源码.zip
更换 tabBarItem 的背景颜色
微信小程序学习用demo:LOOK:点击tabbar变换图标
Tabbar背景图(414*69),在此上传以便使用的时候容易得到
微信小程序——自定义tabbar(截图+源码).zip 微信小程序——自定义tabbar(截图+源码).zip 微信小程序——自定义tabbar(截图+源码).zip 微信小程序——自定义tabbar(截图+源码).zip 微信小程序——自定义...
IOS应用源码之100多个tabBar icons图标资源(shop71713971.taobao.com).zipIOS应用例IOS应用源码之100多个tabBar icons图标资源(shop71713971.taobao.com).zipIOS应用例 1.适合学生学习研究参考 2.适合个人学习研究...
纯代码实现的TabBar,可以根据需要修改TabBar的大小,图片,背影色,任意添加TabBarItem的个数
有封装,中间大图标present VC
微信小程序-点击tabbar变换图标Demo源码
iOS tabbar图片资源 不需要在找美工切图了,自己写demo的时候就可以使用,你说爽不爽。iOS tabbar图片资源 不需要在找美工切图了,自己写demo的时候就可以使用,你说爽不爽。
微信小程序/网页/app/android等各种bar图标导航图标文章图标标题图标下拉/检索收藏上传客服等图标整理
微信小程序开发之tabbar图标和颜色的实现.docx