【问题标题】:Setting titles programmatically in a UITabBarController在 UITabBarController 中以编程方式设置标题
【发布时间】:2014-05-08 18:16:00
【问题描述】:

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】:

您可以在 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;
}

【讨论】:

  • 我最终继承了 UITabBarController。但我所做的看起来与您的建议相似。
猜你喜欢
  • 2020-05-31
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多