我一直在研究如何在我的视图控制器中以编程方式添加选项卡栏,因为如果我不使用滚动视图,就无法将其放在视图的中间。 我对如何添加它感到有些困惑。它需要在我的ViewDidLoad
方法或AppDelegate
中初始化吗?
如果我有:
UITabBarController *tabBar = [[UITabBarController alloc] init];
[self.view addSubview:tabBar.view];
[tabBar release];
如何将它分配到我的scrollview
的底部?
谢谢!
现在在我的appDelegate类中:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
ViewController* vc = [[ViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil];
tabBarController.viewControllers = controllers;
[_window addSubview:tabBarController.view];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
它崩溃了,我不确定是否缺少某个版本的发布。
编辑:对于其他人想知道的,在Appdelegate.m文件中:
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];
NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil];
你为什么要添加tabBarController
? - BhavintabBarController
添加到了你的viewControllers
数组中,这是错误的。这就是我在我的答案中添加的内容。 - Bhavin