2013-01-02 60 views
3

我有一個三個選項卡UITabBar其中設置了每個選項卡的背景以及選項卡本身的背景,如下面的第一張圖所示。每個UITabBarItem都有其設定的FinishedSelectedImageFinishedUnselectedImage,以及其標題NSString。這些圖像是字形(圖標)。UITabBar自定義不按預期方式工作

但是,將selectionIndicatorImage設置爲尺寸爲106x48px的圖像時,在處於其選定狀態時(參見第二張照片),第三個標籤右側有〜1px的空間。

請你能告訴我如何刪除額外的空間嗎?我曾嘗試使用尺寸爲1px的圖像,但這沒有什麼區別。

編輯---我讀到這可能是一個問題,由於大小不正確的selectionIndicatorImage,但我可以證實,這不是一個修復 - 我嘗試使用107x49px圖像,這也沒有什麼區別。

最大。

標籤欄(應該如何選擇時)

標籤欄(它是如何顯示出來的第三個選項卡上 - 注意右邊的1px的)

// customise the UITabBar - set the background image and the selected background image 
[self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bar"]]; 
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selection_bg_3"]]; 


// this is in a for loop. 
UITabBarItem *item = [[UITabBarItem alloc] init]; 
[item setTitle:[titles objectAtIndex:index]]; 
[item setImage:[UIImage imageNamed:[image_titles objectAtIndex:index]]]; 
[item setFinishedSelectedImage:[UIImage imageNamed:[[image_titles objectAtIndex:index] stringByAppendingString:@"_selected"]] withFinishedUnselectedImage:[UIImage imageNamed:[image_titles objectAtIndex:index]]]; 
[navigationController setTabBarItem:item]; 
[item release]; 
+0

我錯了,或者它看起來像在左邊作物顯示出最後一張圖片上右邊的一部分嗎? – tiguero

+0

不確定你的問題。但我愛你的形象!你自己做了嗎?我想要那樣的東西。 :) – Josiah

+0

哈哈,謝謝你,其實我的客戶的設計師設計了它:)如果你願意,我可以給你他的細節嗎? –

回答

2

我結束了子類化UITabBarController使用三個UIImageViews作爲每個標籤的selectionIndicatorBackground,然後使用另一個UIImageView來顯示標籤的標誌符號,以及用於顯示其標題的UILabel。

下面是代碼:

@interface LPTabBarController : UITabBarController 

@property (nonatomic, retain) UIImageView *firstTab, *secondTab, *thirdTab, *firstTabBG, *secondTabBG, *thirdTabBG; 
@property (nonatomic, retain) UILabel *firstTabLabel, *secondTabLabel, *thirdTabLabel; 

@end 


@implementation LPTabBarController 

@synthesize firstTab, secondTab, thirdTab, firstTabBG, secondTabBG, thirdTabBG, firstTabLabel, secondTabLabel, thirdTabLabel; 

- (id) init { 

    if ((self = [super init])) { 

     [self initialise]; 

    } 

    return self; 

} 

- (void) setViewControllers:(NSArray *)viewControllers { 

    [self setViewControllers:viewControllers animated:NO]; 

} 

- (void) setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated { 

    [super setViewControllers:viewControllers animated:animated]; 

    for (UITabBarItem *item in self.tabBar.items) { 

     [item setEnabled:NO]; 

    } 

} 

- (void) initialise { 

    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.tabBar.frame.origin.y - 10, 320, 59)]; 
    [bg setUserInteractionEnabled:YES]; 
    [bg setImage:[UIImage imageNamed:@"tab_bar"]]; 

    CGFloat widthbg = ceilf(self.view.frame.size.width/3); 
    CGFloat heightbg = self.tabBar.frame.size.height; 
    CGFloat width = 23, y = 5; 

    UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; 

    self.firstTab = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_bar_user"]] autorelease]; 
    [self.firstTab setFrame:CGRectMake(ceilf((widthbg - width)/2), y, 23, 23)]; 

    self.firstTabLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 27, widthbg, 20)] autorelease]; 
    [self.firstTabLabel setTextAlignment:NSTextAlignmentCenter]; 
    [self.firstTabLabel setText:@"Profile"]; 
    [self.firstTabLabel setFont:[UIFont boldSystemFontOfSize:13]]; 
    [self.firstTabLabel setTextColor:RGB(80, 23, 17)]; 
    [self.firstTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]]; 
    [self.firstTabLabel setShadowOffset:CGSizeMake(0, 1)]; 
    [self.firstTabLabel setBackgroundColor:[UIColor clearColor]]; 

    self.firstTabBG = [[[UIImageView alloc] initWithImage:nil] autorelease]; 
    [self.firstTabBG setTag:01]; 
    [self.firstTabBG setFrame:CGRectMake(0, 10, widthbg, heightbg)]; 
    [self.firstTabBG addSubview:self.firstTab]; 
    [self.firstTabBG addSubview:self.firstTabLabel]; 
    [self.firstTabBG setUserInteractionEnabled:YES]; 
    [self.firstTabBG addGestureRecognizer:gest]; 
    [bg addSubview:self.firstTabBG]; 

    [gest release]; 

    gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; 

    self.secondTab = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_bar_explore_selected"]] autorelease]; 
    [self.secondTab setFrame:CGRectMake(ceilf((widthbg - width)/2), y, 23, 23)]; 

    self.secondTabLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 27, widthbg, 20)] autorelease]; 
    [self.secondTabLabel setTextAlignment:NSTextAlignmentCenter]; 
    [self.secondTabLabel setText:@"Explore"]; 
    [self.secondTabLabel setFont:[UIFont boldSystemFontOfSize:13]]; 
    [self.secondTabLabel setTextColor:RGB(80, 23, 17)]; 
    [self.secondTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]]; 
    [self.secondTabLabel setShadowOffset:CGSizeMake(0, 1)]; 
    [self.secondTabLabel setBackgroundColor:[UIColor clearColor]]; 

    self.secondTabBG = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_bar_selected_bg"]] autorelease]; 
    [self.secondTabBG setTag:02]; 
    [self.secondTabBG setUserInteractionEnabled:YES]; 
    [self.secondTabBG addGestureRecognizer:gest]; 
    [self.secondTabBG setFrame:CGRectMake(widthbg, 10, widthbg, heightbg)]; 
    [self.secondTabBG addSubview:self.secondTab]; 
    [self.secondTabBG addSubview:self.secondTabLabel]; 
    [bg addSubview:self.secondTabBG]; 

    [self tapped:gest]; 
    [gest release]; 

    gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; 

    self.thirdTab = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_bar_settings"]] autorelease]; 
    [self.thirdTab setFrame:CGRectMake(ceilf((widthbg - width)/2), y, 23, 23)]; 

    self.thirdTabLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 27, widthbg, 20)] autorelease]; 
    [self.thirdTabLabel setTextAlignment:NSTextAlignmentCenter]; 
    [self.thirdTabLabel setText:@"Settings"]; 
    [self.thirdTabLabel setFont:[UIFont boldSystemFontOfSize:13]]; 
    [self.thirdTabLabel setTextColor:RGB(80, 23, 17)]; 
    [self.thirdTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]]; 
    [self.thirdTabLabel setShadowOffset:CGSizeMake(0, 1)]; 
    [self.thirdTabLabel setBackgroundColor:[UIColor clearColor]]; 

    self.thirdTabBG = [[[UIImageView alloc] initWithImage:nil] autorelease]; 
    [self.thirdTabBG setTag:03]; 
    [self.thirdTabBG setFrame:CGRectMake(widthbg * 2, 10, widthbg, heightbg)]; 
    [self.thirdTabBG addSubview:self.thirdTab]; 
    [self.thirdTabBG addSubview:self.thirdTabLabel]; 
    [self.thirdTabBG setUserInteractionEnabled:YES]; 
    [self.thirdTabBG addGestureRecognizer:gest]; 
    [bg addSubview:self.thirdTabBG]; 
    [gest release]; 

    [self.view addSubview:bg]; 
    [bg release]; 

} 

static int lastIndex = 2; 

- (void) tapped:(UITapGestureRecognizer *) gest { 

    switch (lastIndex) { 
     case 1: 
      [self.firstTab setImage:[UIImage imageNamed:@"tab_bar_user"]]; 
      [self.firstTabBG setImage:nil]; 
      [self.firstTabLabel setTextColor:RGB(80, 23, 17)]; 
      [self.firstTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]]; 
      [self.firstTabLabel setShadowOffset:CGSizeMake(0, 1)]; 
      break; 

     case 2: 
      [self.secondTab setImage:[UIImage imageNamed:@"tab_bar_explore"]]; 
      [self.secondTabBG setImage:nil]; 
      [self.secondTabLabel setTextColor:RGB(80, 23, 17)]; 
      [self.secondTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]]; 
      [self.secondTabLabel setShadowOffset:CGSizeMake(0, 1)]; 

      break; 

     case 3: 
      [self.thirdTab setImage:[UIImage imageNamed:@"tab_bar_settings"]]; 
      [self.thirdTabBG setImage:nil]; 
      [self.thirdTabLabel setTextColor:RGB(80, 23, 17)]; 
      [self.thirdTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]]; 
      [self.thirdTabLabel setShadowOffset:CGSizeMake(0, 1)]; 
      break; 
    } 

    switch (gest.view.tag) { 
     case 1 :{ 

      [self setSelectedIndex:0]; 
      [self.firstTab setImage:[UIImage imageNamed:@"tab_bar_user_selected"]]; 
      [self.firstTabBG setImage:[UIImage imageNamed:@"tab_bar_selected_bg"]]; 
      [self.firstTabLabel setTextColor:RGB(252, 208, 170)]; 
      [self.firstTabLabel setShadowColor:RGB(80, 23, 17)]; 
      [self.firstTabLabel setShadowOffset:CGSizeMake(0, -1)]; 

      break; 

     } 

     case 2: { 

      [self setSelectedIndex:1]; 
      [self.secondTab setImage:[UIImage imageNamed:@"tab_bar_explore_selected"]]; 
      [self.secondTabBG setImage:[UIImage imageNamed:@"tab_bar_selected_bg"]]; 
      [self.secondTabLabel setTextColor:RGB(252, 208, 170)]; 
      [self.secondTabLabel setShadowColor:RGB(80, 23, 17)]; 
      [self.secondTabLabel setShadowOffset:CGSizeMake(0, -1)]; 
      break; 

     } 

     case 3: { 

      [self setSelectedIndex:2]; 
      [self.thirdTab setImage:[UIImage imageNamed:@"tab_bar_settings_selected"]]; 
      [self.thirdTabBG setImage:[UIImage imageNamed:@"tab_bar_selected_bg"]]; 
      [self.thirdTabLabel setTextColor:RGB(252, 208, 170)]; 
      [self.thirdTabLabel setShadowColor:RGB(80, 23, 17)]; 
      [self.thirdTabLabel setShadowOffset:CGSizeMake(0, -1)]; 
      break; 

     } 
    } 

    lastIndex = gest.view.tag; 

} 

- (void) dealloc { 

    [firstTab release]; 
    [firstTabBG release]; 
    [secondTab release]; 
    [secondTabBG release]; 
    [thirdTab release]; 
    [thirdTabBG release]; 
    [firstTabLabel release]; 
    [secondTabLabel release]; 
    [thirdTabLabel release]; 
    [super dealloc]; 

}