1

我有一个针对 ios 6 的通用选项卡式应用程序,带有 4 个选项卡。几乎准备好发布了,我开始寻找支持 iAd 的最新和最好的方法。在查看了 Apple 的 iAdSuite 演示项目后,似乎使用 BannerViewController 来包装我的 4 个 UIViewController 是完美的解决方案。

而且它在正确显示 iAd 方面非常有效。

但是,自从进行更改后,我的标签栏不再显示图像/图标。我逐行浏览了 BannerViewController 代码,但看不到任何似乎覆盖图像设置的内容。

我所做的更改是:

  1. 将 BannerViewController.h & .m 添加到我的项目中。
  2. #import <iAd/iAd.h> 在我的 4 个 UIViewController 中。
  3. 修改设置我的选项卡视图控制器的 AppDelegate 代码,使其更改:

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = @[
                [[BannerViewController alloc] initWithContentViewController:viewController1],
                [[BannerViewController alloc] initWithContentViewController:viewController2],
                [[BannerViewController alloc] initWithContentViewController:viewController3],
                [[BannerViewController alloc] initWithContentViewController:viewController4]
                 ];

在我的 4 个视图控制器中,我在 initWithNibName 方法中有以下代码(带有适当的标题和图像名称:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Library", @"Library");
        self.tabBarItem.image = [UIImage imageNamed:@"96-book"];
    }
    return self;
}

使用原始代码,标题和图像按预期工作。使用修改后的代码,我得到标题但没有图像,只有一个黑色按钮。有没有人遇到过这个?

请注意,我在 Apple 示例应用程序中进行了尝试,但也无法在其中显示图像,示例应用程序针对 iOS5,而我的应用程序针对 iOS6。

4

2 回答 2

1

所以我终于找到了一个解决方案,这要归功于这个有点相关的SO问题......

iOS在UITabBar中更改UITabBarButton的框架

我从视图控制器中删除了图像设置代码,而是在将视图控制器分配给 UITabBar 之后将以下行添加到 AppDelegate

UITabBar *tabBar = self.tabBarController.tabBar;
[[tabBar.items objectAtIndex:0] setImage:[UIImage imageNamed:@"253-person"]];
[[tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:@"138-scales"]];
[[tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"85-trophy"]];
[[tabBar.items objectAtIndex:3] setImage:[UIImage imageNamed:@"96-book"]];

现在我的标签栏图像又回来了!

于 2013-02-27T13:37:26.180 回答
0

我刚刚遇到了同样的问题。原因是封装了各个ViewController的BannerViewController没有通过访问tabBarItem,所以UITabBarController找不到自己需要的图标。

正确的解决方法是将其添加到 BannerViewController.m:

- (UITabBarItem *)tabBarItem {
  return _contentController.tabBarItem; 
}
于 2015-04-20T19:28:52.190 回答