问题
I want to put a UITabBar in two views, but not in the MainViewController, because I am not using the AppDelegate to put it.
Is there a solution without .xib or storyboard
回答1:
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];
来源:https://stackoverflow.com/questions/22195806/how-to-add-uitabbarcontroller-programatically-without-using-appdelegate