【问题标题】:iOS 7 Tab bar icons temporarily disappear when on More tab在更多选项卡上时,iOS 7 选项卡栏图标暂时消失
【发布时间】:2025-12-07 00:50:02
【问题描述】:

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

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

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

这是我的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

【问题讨论】:

    标签: ios7 uinavigationcontroller uitabbar uitabbaritem


    【解决方案1】:

    天哪,在这个错误上花了这么多时间,但这是我的解决方法。而不是:

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

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

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

    【讨论】:

    • 我刚刚遇到了同样的问题。不过,我发现我可以在 UINavigationController 初始化之后设置 tabBarItem,而不是子类化。这可能只是我的特殊情况,但如果其他人遇到这个问题,他们也可以尝试另一种选择。
    • @kailoon 这是在 iOS 7 上,就像我的问题一样吗?
    最近更新 更多