来自会员z304983615的分享:
网上有海量的关于创建UITabBarController实例的代码,不过99%都是在AppDelegate中创建并作为App的根视图。但是,并不见得UITabBarController就一定要作为App的根视图存在。下面提供的这个demo就是在某个视图控制器中创建出来的(且标签的viewControllers全都是导航栏控制器),并且该视图控制器将做为一个UINavigationController的rootViewController,然后将这个UINavigationController的view加到其他别的视图控制器的view中。从而实现了RT所述的UINavigationController+UITabBarController多层嵌套。
不过,在UI上有个小Bug,就是显示出来的标签栏控制器的view会下移一段状态栏的高度。楼主为这个真是百思不得其解阿。 但是,最后设置了UITabBarController的view的frame为(0,-20,320,480)才将问题解决(设置为(0,0,320,460)不能解决问题)。
下面是部分代码
用UINavigationController嵌套带有UITabBarController的视图控制器:
-(void)viewDidLoad
{
[super viewDidLoad];
if (mainNav == nil) {
MainViewController *mainViewController = [[MainViewControlleralloc] initWithNibName:nil bundle:nil];
mainNav = [[UINavigationController alloc]initWithRootViewController:mainViewController];
[mainViewController release];
}
mainNav.view.frame = CGRectMake(0.0, 0.0, 320.0, 460.0);
mainNav.view.backgroundColor= [UIColor blackColor];
[self.view addSubview:mainNav.view];
}
实例化UITabBarController:
-(void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden= YES;
if (tabBarCon == nil) {
tabBarCon = [[UITabBarController alloc] init];
FirstViewController *firstViewController =[[FirstViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *firstNav =[[UINavigationController alloc]initWithRootViewController:firstViewController];
SecondViewController *secondViewController =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *secondNav =[[UINavigationController alloc]initWithRootViewController:secondViewController];
tabBarCon.viewControllers = [NSArrayarrayWithObjects:firstNav,secondNav, nil];
}
/*******************************************************************************************************/
tabBarCon.view.frame= CGRectMake(0.0, -20.0, 320.0, 480.0);// 这句很关键,不加这句将会有一段状态栏高度的空白区域
/*******************************************************************************************************/
[self.view addSubview:tabBarCon.view];
}
附demo:
原帖:http://www.cocoachina.com/bbs/read.php?tid=140083
CocoaChina是全球最大的苹果开发中文社区,官方微信每日定时推送各种精彩的研发教程资源和工具,介绍app推广营销经验,最新企业招聘和外包信息,以及Cocos2d引擎、CocosStudio开发工具包的最新动态及培训信息。关注微信可以第一时间了解最新产品和服务动态,微信在手,天下我有!
请搜索微信号“CocoaChina”关注我们!