在“更多”选项卡上,iOS 7标签栏图标暂时消失

时间:2014-04-25 16:20:50

标签: ios7 uinavigationcontroller uitabbar uitabbaritem

当我将导航控制器嵌入的视图控制器添加到标签栏时,当返回“更多”选项卡时,其图标+标题会短暂消失。

但是当这样添加视图控制器时,图标+图像就可以了,不会消失。

我已经尝试了很多东西,而且我没有选择。有什么想法吗?

这是我的AppDelegate代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.tabBarController = [[UITabBarController alloc] init];

    // Must be placed here, just before tabs are added.  Otherwise navigation bar
    // will overlap with status bar.
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];

    [self addViewControllersToTabBar];
    self.window.rootViewController = self.tabBarController;
    self.window.backgroundColor    = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)addViewControllersToTabBar
{
    NSArray* tabBarClassNames =
    @[
      NSStringFromClass([FirstViewController  class]),
      NSStringFromClass([SecondViewController class]),
      NSStringFromClass([FirstViewController  class]),
      NSStringFromClass([FirstViewController  class]),
      NSStringFromClass([FirstViewController  class]),
      NSStringFromClass([SecondViewController class]),
      NSStringFromClass([FirstViewController  class]),
      ];

    NSMutableArray* viewControllers = [NSMutableArray array];
    for (NSString* className in tabBarClassNames)
    {
        UIViewController*       viewController = [[NSClassFromString(className) alloc] init];
        UINavigationController* navigationController;

        navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

        [viewControllers addObject:navigationController];
    }

    [viewControllers addObject:[[FirstViewController alloc] init]]; // This one is fine.

    self.tabBarController.viewControllers        = viewControllers;
    self.tabBarController.selectedViewController = viewControllers[2];
}

并且视图控制器实际上只不过是:

@implementation SecondViewController

- (instancetype)init
{
    if (self = [super init])
    {
        self.title            = @"second";
        self.tabBarItem.image = [UIImage imageNamed:@"second.png"];
    }

    return self;
}

@end

enter image description here

1 个答案:

答案 0 :(得分:0)

神圣的狗屎,花了很多时间在这个bug上,但这是我的解决方法。而不是:

navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

我创建了自己的NavigationController作为UINavigationController的子类:

@implementation NavigationController

- (instancetype)initWithRootViewController:(UIViewController*)rootViewController
{
    if (self = [super initWithRootViewController:rootViewController])
    {
        NSString* className = NSStringFromClass([rootViewController class]);
        NSString* name      = [className stringByReplacingOccurrencesOfString:@"ViewController" withString:@""];

        self.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Tab.png", name]];
    }

    return self;
}

@end

然后执行:

navigationController = [[NavigationController alloc] initWithRootViewController:viewController];

先决条件是选项卡图像与视图控制器类名具有相同的基本名称,这在我的应用程序中已经是这种情况。

我在视图控制器中设置self.tabBarItem.image' init方法,这似乎导致我看到的效果。因此,除了使用我自己的导航控制器之外,我还简单地删除了在每个单独的视图控制器中设置tabBarItem