UITabBarController+UINavigationController多层嵌套

 

 来自会员z304983615的分享:

网上有海量的关于创建UITabBarController实例的代码,不过99%都是在AppDelegate中创建并作为App的根视图。但是,并不见得UITabBarController就一定要作为App的根视图存在。下面提供的这个demo就是在某个视图控制器中创建出来的(且标签的viewControllers全都是导航栏控制器),并且该视图控制器将做为一个UINavigationControllerrootViewController,然后将这个UINavigationControllerview加到其他别的视图控制器的view中。从而实现了RT所述的UINavigationController+UITabBarController多层嵌套。

 

不过,在UI上有个小Bug,就是显示出来的标签栏控制器的view会下移一段状态栏的高度。楼主为这个真是百思不得其解阿。 但是,最后设置了UITabBarControllerviewframe(0,-20,320,480)才将问题解决(设置为(00320460)不能解决问题)

 

下面是部分代码

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

/cms/uploads/soft/130830/4196-130S0161214.zip

 

原帖:http://www.cocoachina.com/bbs/read.php?tid=140083

CocoaChina是全球最大的苹果开发中文社区,官方微信每日定时推送各种精彩的研发教程资源和工具,介绍app推广营销经验,最新企业招聘和外包信息,以及Cocos2d引擎、CocosStudio开发工具包的最新动态及培训信息。关注微信可以第一时间了解最新产品和服务动态,微信在手,天下我有!

请搜索微信号“CocoaChina”关注我们!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值