關於UITabBar各部分自訂的程式碼片段

來源:互聯網
上載者:User

標籤: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];




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.