關於UITabBar各部分自定義的代碼片段


一、自定義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];



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM