UITabBarController+UINavigationController多层嵌套

网上有海量的关于创建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 = [[MainViewController alloc] 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 = [NSArray arrayWithObjects: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值