TabBar上的图像不能正确显示..在顶部显示一个额外的行 – UPDATED

我已经添加了一个ImageView作为我的TabBar(有三个NavigationControllers)的子视图。 当我点击tabBarController的任何选项卡,然后imageView上的图像相应地改变(图像显示特定的选项卡和其他人未选中)。

但是,图像总是在tabBar上显示额外的行。 它看起来像跨越了tabBar的限制。 我的图像尺寸是320×64像素(非视网膜iPhone)和640×128像素(视网膜iPhone)。

以下是我如何声明图像视图和tabBarController的实例var。

@interface HomePageViewController ()<UITabBarControllerDelegate> { UIImageView* tabBarView; UITabBarController *tabBarController; } -(UITabBarController *) configureTheTabBarControllerWithNavControllerAtIndex:(NSInteger)index { UINavigationController *customerCareNavController; UINavigationController *accAndContactsNavController; UINavigationController *purchaseOrderNavController; CustomerCareViewController *custCareVC; PurchaeOrderViewController *POController; AccountsAndContactsViewController *accAndContactsController; custCareVC = [[CustomerCareViewController alloc] initWithNibName:@"CustomerCareViewController_iPhone" bundle:NULL]; POController = [[PurchaeOrderViewController alloc] initWithNibName:@"PurchaeOrderViewController_iPhone" bundle:NULL]; accAndContactsController = [[AccountsAndContactsViewController alloc] initWithNibName:@"AccountsAndContactsViewController_iPhone" bundle:NULL]; customerCareNavController = [[UINavigationController alloc] initWithRootViewController:custCareVC]; purchaseOrderNavController = [[UINavigationController alloc] initWithRootViewController:POController]; accAndContactsNavController = [[UINavigationController alloc] initWithRootViewController:accAndContactsController]; tabBarController = [[UITabBarController alloc] init]; tabBarController.viewControllers = [NSArray arrayWithObjects:customerCareNavController,accAndContactsNavController,purchaseOrderNavController, nil]; switch (index) { case 0: tabBarController.selectedIndex = 0; break; case 1: tabBarController.selectedIndex = 1; break; case 2: tabBarController.selectedIndex = 2; break; } tabBarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_mypeople.png"]]; tabBarView.frame = CGRectMake(0, -15, 320, 64); [tabBarController.tabBar addSubview:tabBarView]; tabBarController.delegate = self; [self selectTabBarIndex:index]; [self presentViewController:tabBarController animated:YES completion:NULL]; return tabBarController; } -(void)tabBarController:(UITabBarController *)TBController didSelectViewController:(UIViewController *)viewController { NSUInteger indexSelected = [[TBController viewControllers] indexOfObject:viewController]; [self selectTabBarIndex:indexSelected]; } - (void) selectTabBarIndex:(NSInteger)index { switch (index) { case 0: tabBarView.image=[UIImage imageNamed:@"tab_myCalendar.png"]; break; case 1: tabBarView.image=[UIImage imageNamed:@"tab_myDetails.png"]; break; case 2: tabBarView.image=[UIImage imageNamed:@"TabBarItem_PO.png"]; break; } } 

请看截图

在这里输入图像说明

设置barStyle为黑色给我以下结果

在这里输入图像说明

线已经褪色了一点,但仍然可见

嘿,我试了一下,它的作品

 tabBarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_mypeople.png"]]; tabBarView.frame = CGRectMake(0, 0, 320, tabBarController.tabBar.frame.size.height); 

但是,图像显示有点拉长..

写这个没有拉伸:这将像魅力工作..!

 tabBarController.tabBar.backgroundImage = [UIImage new]; tabBarController.tabBar.shadowImage = [UIImage new]; tabBarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_mypeople.png"]]; tabBarView.frame = CGRectMake(0, -15, 320, 64); [tabBarController.tabBar addSubView:tabBarView]; 

下面的代码将从tabbar中删除行…

  if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) { tabbarController.tabBar.barStyle=UIBarStyleBlackOpaque; } 

将图像作为子视图添加到图像视图中,而不是将图像作为子视图的背景图像添加,并使阴影图像为空图像(线是阴影图像)。

 tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tab_mypeople.png"]; tabBarController.tabBar.shadowImage = [UIImage new]; 

如果出于某种原因,您仍然需要使用子视图而不是背景图像,则可以继续执行问题中的操作,但将背景图像和阴影图像都设为空白图像(无法设置一个自定义的阴影图像,也没有设置自定义的背景图像)。 这将摆脱那阴影图像线。

 tabBarController.tabBar.backgroundImage = [UIImage new]; tabBarController.tabBar.shadowImage = [UIImage new]; tabBarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_mypeople.png"]]; tabBarView.frame = CGRectMake(0, -15, 320, 64); [tabBarController.tabBar addSubview:tabBarView]; 

写在应用程序的代表

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions UIImage *tabBackground = [[UIImage imageNamed:@"tab_bg"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [[UITabBar appearance] setBackgroundImage:tabBackground]; [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_select_indicator"]];