2011-11-10 30 views
0

在此先感謝。 我想添加兩個頂部和另一個底部的標籤欄控制器,如enter image description here 這個。在iphone中可能嗎?如果可能,任何人都知道請幫助我做到這一點。與iphone中的tabbar控件相關的問題

+0

我想這是不可能的,你需要自定義標籤欄。 – iOSPawan

回答

0

據我所知,應該可以做到這一點。只需添加一個到你的視圖,另一個添加到你的rootview控制器,在不同的位置,你應該能夠鏈接到不同的地方。如果沒有,只需拉起photoshop,「製作」標籤欄並將按鈕放在它下面以模擬效果。

PS,薩曼莎太美了!哈哈

0

UITabBar難以在任何方向上修改。

許多easyer將被你自己的,自定義的替換它。 Offcorse,它可以是任何你喜歡的。看看這個:

//CustomTabBar.h: 

#import <Foundation/Foundation.h> 

@interface CustomTabBar : UITabBarController { 
    UIButton *SHOPPING; 
    UIButton *DINING; 
    UIButton *MAPS; 
    UIButton *PARKING; 
    UIButton *PROMOTION; 
    UIImageView *BarBackground; 
} 
-(void) addCustomElements; 
-(void) hideExistingTabBar; 
-(void) selectTab:(int)tabID; 
@end 

//CustomTabBar.m: 
#import "CustomTabBar.h" 

@implementation CustomTabBar 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    [self hideExistingTabBar]; 
    [self addCustomElements]; 
} 

- (void)hideExistingTabBar 
{ 
    for(UIView *view in self.view.subviews) 
    { 
     if([view isKindOfClass:[UITabBar class]]) 
     { 
      view.hidden = YES; 
      break; 
     } 
    } 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

-(void)addCustomElements 
{ 
    //in your case you will need 2 bars, but I needed only one. Just add another one. 
    BarBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BarBackground.jpg"]]; 
    BarBackground.frame = CGRectMake(0, 430, 320, 50); 

    // Initialise our two images 
    UIImage *btnImage = [UIImage imageNamed:@"BarShipping.png"]; 
    UIImage *btnImageSelected = [UIImage imageNamed:@"BarShipping.png"]; 

    SHOPPING = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button 
    SHOPPING.frame = CGRectMake(0, 430, 64, 50); // Set the frame (size and position) of the button) 
    [SHOPPING setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button 
    [SHOPPING setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button 
    [SHOPPING setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed. 
    [SHOPPING setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially 

    // Now we repeat the process for the other buttons 
    btnImage = [UIImage imageNamed:@"BarDinning.png"]; 
    btnImageSelected = [UIImage imageNamed:@"BarDinning.png"]; 
    DINING = [UIButton buttonWithType:UIButtonTypeCustom]; 
    DINING.frame = CGRectMake(64, 430, 64, 50); 
    [DINING setBackgroundImage:btnImage forState:UIControlStateNormal]; 
    [DINING setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
    [DINING setTag:1]; 
    <...> 


    // Add my new buttons to the view 
    [self.view addSubview:BarBackground]; 
    [self.view addSubview:SHOPPING]; 
    [self.view addSubview:DINING]; 
    <...> 


    // Setup event handlers so that the buttonClicked method will respond to the touch up inside event. 
    [SHOPPING addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [DINING addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    <...> 
} 

- (void)selectTab:(int)tabID 
{ 
    //if you will need navigation controller, or something, you will need to work on this part, or it will just crash. 
    switch(tabID) 
    { 
     case 0: 
      [SHOPPING setSelected:true]; 
      [DINING setSelected:false]; 
      <...> 
      break; 
     case 1: 
      [SHOPPING setSelected:false]; 
      [DINING setSelected:true]; 
      <...> 
      break; 
     <...> 
    } 

    if (self.selectedIndex == tabID) { 
     UINavigationController *navController = (UINavigationController *)[self selectedViewController]; 
     [navController popToRootViewControllerAnimated:YES]; 
    } else { 
     self.selectedIndex = tabID; 
    } 

} 

- (void)buttonClicked:(id)sender 
{ 
    int tagNum = [sender tag]; 
    [self selectTab:tagNum]; 
} 

@end 

然後,像往常一樣添加和使用TabBarController。你必須做的唯一的事情就是將TabBarController類改爲這個,在你的xib文件中使用CastomTabBar。 (或者用這個類來分配它,如果你添加了它,那麼你的視圖就會被調整大小):UITabBarController * tb = [[CustomTabBar alloc] init];) 還有一個地方,那就是tabBar。如果你使用這個代碼,它將會是,但在平常的tabBar位置 - 在底部。對於第二個tabBar,你需要準備好自己的位置。希望,這會有所幫助。