TabBar上的图像显示不正确..在顶部显示一条额外的行 - 更新

时间:2013-12-17 04:24:28

标签: ios iphone uitabbarcontroller

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

但是,图像始终在tabBar上显示一条额外的行。看起来它超过了tabBar的限制。我的图像尺寸为320x64像素(非视网膜iPhone)和640x128像素(视网膜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;
    }
}

请参阅截图..

enter image description here

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

enter image description here

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

4 个答案:

答案 0 :(得分:2)

嘿,我尝试了一些东西并且有效

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

答案 1 :(得分:1)

下面的代码会删除标签栏中的行...

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

在app delegate中写这个

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