在UITabBarController中以编程方式设置标题

问题描述 投票:1回答:1

[使用UITabBarController,我面临以下问题。我想在每个选项卡中使用给定的字符串作为所有视图控制器的标题。显然无法在storyboard中进行设置。

通过搜索网络,我找到了这个几乎可行的解决方案。将以下类型的代码放入每个单独的视图控制器中。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UITabBarItem *tabBarItem;
    tabBarItem=[[UITabBarItem alloc] initWithTitle:nil
                                             image:[self imageFromText:@"My Tab Title"
                                                              withFont:[UIFont systemFontOfSize:17.0]] tag:0];
    self.tabBarItem=tabBarItem;
}

问题是:仅当用户点击所有选项卡时,所有标题才设置为我想要的。

这当然不是一个很好的永久解决方案。我需要让所有标题在应用启动时正确显示。

ios objective-c uitabbarcontroller uitabbar uitabbaritem
1个回答
0
投票

您可以在AppDelegate中进行更改。

例如如果您有3 TabBarIcons

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

    [[tabBarController.viewControllers objectAtIndex:0] setTitle:@"Your "];
    [[tabBarController.viewControllers objectAtIndex:1] setTitle:@"title "];
    [[tabBarController.viewControllers objectAtIndex:2] setTitle:@"here"];

    [[[tabBarController.viewControllers objectAtIndex:0] tabBarItem] setImage:[[UIImage imageNamed:@"first"] imageWithRenderingMode:UIImageRenderingModeAutomatic]];
    [[[tabBarController.viewControllers objectAtIndex:1] tabBarItem] setImage:[[UIImage imageNamed:@"second"] imageWithRenderingMode:UIImageRenderingModeAutomatic]];
    [[[tabBarController.viewControllers objectAtIndex:2] tabBarItem] setImage:[[UIImage imageNamed:@"third"] imageWithRenderingMode:UIImageRenderingModeAutomatic]];

    return YES;
}
© www.soinside.com 2019 - 2024. All rights reserved.