将UITabBarItems添加到UITabBar

Fra*_*ank 2 uitabbarcontroller uitabbaritem ipad

我希望有人能解释我如何做到这一点:

我有一个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];
Run Code Online (Sandbox Code Playgroud)

Rya*_*yan 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];
Run Code Online (Sandbox Code Playgroud)