在UITabbarController包含的UINavigationController应用中,如果UINavigationController某一页(某个level)需要隐藏Tabbar,之前的做法是在push那一页之前,将那一页的ViewController中的hidesBottombarWhenPushed参数设为YES,这样当那一页push进UINavigationController中时,底部的Tabbar就会隐藏掉。

但是这种方法有一个潜在的而又巨大的问题,假设现在我有3个ViewController A,B,C,其中为Navigation的RootViewController,A中push B,B中push C。这时,如果我想要在B中隐藏Tabbar,而在C中显示Tabbar,根据最朴素的想法就是:

1
2
3
4
5
6
7
8
9
//SecondLevel即为B,此代码在A中进行
SecondLevelViewController *_2vc = [[SecondLevelViewController alloc]initWithNibName:@"SecondLevelViewController" bundle:nil];
    _2vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:_2vc animated:YES]; 
 
//ThirdLevel即为C,此代码在B中进行
ThirdLevelViewController *_3vc = [[ThirdLevelViewController alloc]initWithNibName:@"ThirdLevelViewController" bundle:nil];
    _3vc.hidesBottomBarWhenPushed = NO;
    [self.navigationController pushViewController:_3vc animated:YES];

然而很可惜,虽然A中push B能让Tabbar隐藏,但是B中push C,就没办法让Tabbar显示出来了。这是一个比较郁闷的问题,我只能猜想其中的原理是,当设置某一层hidesBottomBarWhenPushed = YES之后,UITabbarController就像不存在一样,就连调用self.tabbarController或者self.navigationController.tabbarController也应该是nil。

所以解决办法只能是手动隐藏Tabbar。

在B中,加入以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
- (void) hideTabBar:(UITabBarController *) tabbarcontroller {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
        }
    }
    [UIView commitAnimations];
}
 
- (void) showTabBar:(UITabBarController *) tabbarcontroller {
 
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        NSLog(@"%@", view);
 
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
        }
    }
 
    [UIView commitAnimations];
}
 
-(void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   [self hideTabbar:self.tabbarController];
}
 
-(void)viewWillDisAppear:(BOOL)animated
{
   [super viewWillDisAppear:animated];
   [self showTabbar:self.tabbarController];
}

以上代码参考了 http://stackoverflow.com/questions/5272290/how-to-hide-uitabbarcontroller

最新文章

  1. 蒙特卡洛模拟入门的几个小例子(R语言实现)
  2. jquery移除属性值
  3. HDInsight 指定输出目录 insert overwrite
  4. Android开发艺术探索笔记——View(二)
  5. leangoo更换背景、设置颜色标签功能上线啦!
  6. 基于Flot可放缩的折线图
  7. Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config.
  8. JavaFX它ListView使用
  9. Android 中OKHttp请求数据get和post
  10. js时间戳与日期格式的相互转换
  11. 第十五篇-EditText做简单的登录框
  12. jsp实现验证码登陆
  13. gps相关的知识
  14. Android 音视频深入 九 FFmpeg解码视频生成yuv文件(附源码下载)
  15. C#面向对象架构总结
  16. JPQL模糊匹配单个字符
  17. 自学Linux Shell7.3-linux共享文件
  18. URLEncoder 和URLDecoder
  19. django基础2
  20. WPF RegisterAttached ListBoxItem(附加属性传递到Item)

热门文章

  1. 20. 星际争霸之php设计模式--适配器模式
  2. C#属性和变量的区别学习
  3. nginx简单的rewrite配置
  4. Spring RabbitMq
  5. [问题2014S07] 解答
  6. JavaIO总结
  7. 第十二天 jni 了解
  8. 20个Linux服务器安全强化建议(二)
  9. MySQL安装(转)
  10. 简单的SpringMVC的测试项目----跟struts2对比着学习