2

我希望任何人都可以解释我如何做到这一点:

我有一个 TabBar 和两个 TabBarItems,我怎样才能将项目附加到 TabBar。我不是通过 IB 这样做的,因为 TabBar 只适合屏幕,因为项目应该在左侧。

这就是我构建它们的方式:

tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController2 = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

tabBarController.tabBar.frame = CGRectMake(0, 974, 384, 50);    
tabBarController2.tabBar.frame = CGRectMake(384, 974, 384, 50);

UITabBarItem *tbi1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0];
UITabBarItem *tbi2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1];
4

1 回答 1

6

您不会直接在标签栏中设置标签栏项目。相反,您将标签栏项目分配给标签栏tabBarItem控制器包含的每个视图控制器的属性。然后,当您将视图控制器添加到标签栏控制器时,标签栏控制器将为您管理标签栏项目的显示。

UITabBarController * tabBarController = [[UITabBarController alloc] init];

UIViewController * viewController1 = [[YourViewController alloc] init];
UIViewController * viewController2 = [[YourOtherViewController alloc] init];

viewController1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0];
viewController2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1];

tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
于 2010-12-01T17:42:02.410 回答