【问题标题】:Image on TabBar Not displaying properly.. shows an extra line on top - UPDATEDTabBar 上的图像未正确显示.. 在顶部显示多行 - 已更新
【发布时间】:2013-12-17 04:24:28
【问题描述】:

我添加了一个 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;
    }
}

请看截图..

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

线条已经淡了一点,​​但仍然可见..

【问题讨论】:

    标签: ios iphone uitabbarcontroller


    【解决方案1】:

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

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

    【讨论】:

    • 保持下去很有帮助
    【解决方案2】:

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

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

    【讨论】:

    • 这种不透明的样式适合我,请尝试设置其他样式,希望它能解决您的问题 :)
    【解决方案3】:

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

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

    【讨论】:

    • @Coder123,如果使用背景图片不适用于您的情况,我已添加了答案。
    【解决方案4】:

    在应用委托中写这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多