如何在不使用 AppDelegate 的情况下以编程方式添加 UITabBarController



我想把一个UITabBar放在两个视图中,但不放在MainViewController中,因为我没有使用AppDelegate来放置它。

是否有没有 .xib 或故事板的解决方案

UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = @"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = @"SECOND";
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = @[vc1,vc2];
tabBar.selectedIndex   = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];

最新更新