【问题标题】:UITabbarController Change Bar Item Title ProgrammaticallyUITabbarController 以编程方式更改栏项目标题
【发布时间】:2014-04-14 08:04:55
【问题描述】:

我正在尝试以编程方式更改我的应用中标签栏项目的名称。

在我的 storyboard 中,我将 UITabBarController 设置为初始视图,层次结构如下:

UITabBarController -> UINavigationController -> UITableViewController -> 详细视图控制器

要更改栏项目的标题,您需要更改 UINavigationController 的栏项目的标题。在 IB 中一切正常,但我需要本地化我的应用程序,这是我动态执行的(无需重新启动应用程序),每次启动应用程序时,IB 中给出的标题都已设置,并且标题不会更改为他们的本地化标题,直到我点击栏项目。

在我各自的 UITableViewControllers 中,我将标题设置为 self.title = localized title;,效果很好。 但我想让应用在启动时更改栏项目标题,直到我点击它们。

我已经查看了关于此问题的 SO 帖子并尝试了建议,但条形项目的标题仍然始终设置为 IB 中的值。我也在下面尝试了这段代码,但这只会在启动时设置选定的栏项:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Select the desired tab of our initial tab bar controller:
    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
    tabBar.selectedIndex = 1;
    tabBar.navigationController.title = @"Item 2";

    [(UITabBarItem*)[self->tabBarController.tabBar.items objectAtIndex:1] setTitle:@"Item 2"];

    // Override point for customization after application launch.
    return YES;

}

【问题讨论】:

    标签: uitabbarcontroller tabbar items


    【解决方案1】:

    好的,我正在回答我自己的问题。在网上搜索了很长时间后,SO,并尝试了各种组合,我破解了它。下面的帖子帮助我走上了正轨,但并没有解决它(仍然给它+1):

    Tab Bar Item title before appear Storyboard

    在 UITableViewController .m 文件中,您需要添加此代码以在加载或点击标签栏项目之前设置它的标题。

    - (id)initWithCoder:(NSCoder *)decoder {
    
        self = [super initWithCoder:decoder];
        if (self) {
            self.navigationController.tabBarItem.title = NSLocalizedStringFromTableInBundle(@"TITLE_TABLANGUAGE", nil, currentLanguageBundle, @"");
        }
        return self;
    }
    

    答案在于 UITabBarController 的栏项目从 UINavigationController 的栏项目的标题中获取它们的值,在本例中为标题。在 initWithCoder 方法中设置该标题很重要。将其放在其他位置不会在被点击之前设置标题。

    【讨论】:

      【解决方案2】:

      我认为你让它变得比它需要的更困难。要在运行时更改选项卡栏项目的标题,只需从维护相关选项卡的视图控制器中执行以下操作:

      self.navigationController.tabBarItem.title = @"desired title text";
      

      完成。

      【讨论】:

        【解决方案3】:

        直接使用这个函数:

             private func updateTabBarTitles() {
             if let navControllers = viewControllers as? [UINavigationController] {
        
                 let vcs = navControllers.map({$0.viewControllers.first!})
        
                 for vc in vcs {
                     switch vc {
                   case is First:    vc.navigationController?.tabBarItem.title = "firstVCTabbarTitle"
                   case is Second:    vc.navigationController?.tabBarItem.title = "secondVCTabbarTitle"
                   default: break
        
                     }
                 }
             }
         }
        

        【讨论】:

          【解决方案4】:

          在 Swift 5 和 Xcode 11 中

          一个很好的选择是使用 SF Symbols。从Apple website 下载 SF 符号。 然后打开应用程序并选择您喜欢的符号。使用 CMD-Shift-C 复制名称。

          在您的视图控制器上使用它

          profileViewController.tabBarItem = 
             UITabBarItem(
                title: "Profile", 
                image: UIImage(systemName: "person.fill"),
                tag: 2)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-30
            • 1970-01-01
            • 2017-01-14
            • 1970-01-01
            • 2019-03-09
            • 2014-05-08
            • 1970-01-01
            • 2016-02-19
            相关资源
            最近更新 更多