標籤:des style color os io strong for ar
一、自訂TabBar選項卡背景
預設UITabBarController的TabBar背景是黑色的,如何自訂成背景圖片呢?
UITabBarController *tabBarController = [[UITabBarController alloc] init];// 擷取選項卡控制器視圖的所有子視圖,儲存到一數組中NSArray *array = [tabBarController.view subviews];// 索引值為1的應該就是TabBarUITabBar *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];