我有一个三个标签UITabBar
,其中设置了每个标签的背景,以及标签栏本身的背景,如下面第一张图所示。每个UITabBarItem
都设置了FinishedSelectedImage
和FinishedUnselectedImage
,标题为NSString
。这些图像是字形(图标)。
但是,将selectionIndicatorImage
设置为尺寸为106x48像素的图像,当处于选定状态时,第三个标签右侧有~1像素空间(见第二张照片)。
请告诉我如何删除额外空间?我尝试使用宽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 :(得分: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];
}