3

我有一个三个选项卡UITabBar,其中设置了每个选项卡的背景以及选项卡栏本身的背景,如下面的第一张图片所示。每个UITabBarItem都有它的FinishedSelectedImageFinishedUnselectedImage集合,以及NSString它的标题。这些图像是字形(图标)。

但是,将 设置为selectionIndicatorImage大小为 106x48px 的图像后,第三个选项卡的右侧有约 1px 的空间,当处于选中状态时(参见第二张照片)。

请你能告诉我如何删除额外的空间吗?我曾尝试使用宽 1 像素的图像,但这并没有什么区别。

编辑---我读到这可能是由于尺寸不正确的 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];
4

1 回答 1

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];

}
于 2013-01-04T14:43:26.250 回答