博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【原】iOS学习之UITabBar的隐藏
阅读量:6939 次
发布时间:2019-06-27

本文共 3211 字,大约阅读时间需要 10 分钟。

 当页面使用 UITabBarController + UINavigationController 框架的时候,当跳转到详情页面的时候,如果 UITabBar 仍然存在的话就会造成逻辑混乱,用户体验也会下降,因此我们就有一个在详情页将 UITabBar 隐藏的需求,当然,在其他的一些情况也可能有隐藏 UITabBar 的需求, 在这里小编为大家介绍三种隐藏 UITabBar 的方法,大家可以根据详细的需求进行选择。

1、第一种:

 直接隐藏当前页面的 UITabBar

// 显示tabBarself.tabBarController.tabBar.hidden = NO;// 隐藏tabBarself.tabBarController.tabBar.hidden = YES;

2、第二种:

 将 push 到的页面的 UItabBar 隐藏

// 在push跳转时隐藏tabBarUIViewController *vc2 = [UIViewController new];vc2.hidesBottomBarWhenPushed = YES;[vc1 pushViewController:vc2 animated:YES];

 该方法在push页面的时候使用,有一定的局限性,根据其名字可以发现,只有在 push跳转的时候才会生效,也就是说在 UITabBarController 和 UINavigationController 结合使用的时候能用。

 这也正是小编子在开篇时提到的那种情况,小编个人觉得也是比较常用的一种情况!

3、第三种:

 不使用系统提供的现有方法,自定义方法,修改 TabBar 的 subview 的 frame 就行了

 原理:

  UITabBarController的subview 共有两个,一个叫 UITabBar,就是底下的那个 Bar;另一个叫UITranstionview,就是 Bar 上面的视图。这两个 view 下面还有其他的subview,这就不用去管它了。把UITabBar的 y 向下移49个单位,把UITranstionview 的 hight 加长 49 个单位。

 代码1:

- (void)hidesTabBar:(BOOL)hidden{          [UIView beginAnimations:nil context:NULL];     [UIView setAnimationDuration:0];          for (UIView *view in self.tabBarController.view.subviews) {         if ([view isKindOfClass:[UITabBar class]]) {             if (hidden) {                 [view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height, view.frame.size.width , view.frame.size.height)];                             }else{                 [view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height - 49, view.frame.size.width, view.frame.size.height)];                              }         }else{             if([view isKindOfClass:NSClassFromString(@"UITransitionView")]){                 if (hidden) {                     [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height)];                                      }else{                     [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 49 )];                                     }             }         }     }     [UIView commitAnimations];      }

 代码2:

-(void)makeTabBarHidden:(BOOL)hide { // Custom code to hide TabBar    if ( [self.tabBarController.view.subviews count] < 2 )    {        return;    }    UIView *contentView; if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )    {        contentView = [self.tabBarController.view.subviews objectAtIndex:1];    } else {            contentView = [self.tabBarController.view.subviews objectAtIndex:0];    }    if (hide) {        contentView.frame = self.tabBarController.view.bounds;    } else {            contentView.frame = CGRectMake(self.tabBarController.view.bounds.origin.x, self.tabBarController.view.bounds.origin.y,                                           self.tabBarController.view.bounds.size.width, self.tabBarController.view.bounds.size.height -                                           self.tabBarController.tabBar.frame.size.height);    }    self.tabBarController.tabBar.hidden = hide;}

 

以上是小编总结的三种方法,也是从各位大神的博客总结的,如果有什么新的方法,欢迎一起讨论!

转载于:https://www.cnblogs.com/gfxxbk/p/5595335.html

你可能感兴趣的文章
How to Properly Remove Datastore or LUN from ESXi 5.x hosts
查看>>
[读书]读《重构-改善既有代码的设计》
查看>>
我的友情链接
查看>>
一次owa登录exchange邮箱提示下载owaauth.dll的解决办法
查看>>
我的友情链接
查看>>
Log.cat 的使用
查看>>
使用myEclipse把java项目上传到码云
查看>>
Python爬虫框架Scrapy 学习笔记 5 ------- 使用pipelines过滤敏感词
查看>>
GTM+800的时间格式转成yyyy-mm-dd的格式
查看>>
QT下载地址
查看>>
sap system monitor ---- SM51 function
查看>>
网络编程服务器基础模型三(多线程模型)
查看>>
C++ MFC WebBrowser 探索(三)
查看>>
apache 禁止爬虫
查看>>
debian下LAMP+nginx代理+awstats+cacti+nagios(一)
查看>>
lib_mysqludf_json导致mysql重启原因分析
查看>>
nginx+onethink配置访问路径
查看>>
DB2_COMPATIBILITY_VECTOR=ORA, DATE type
查看>>
ip classless
查看>>
Exchange Server 2010 OWA与Lync Server 2010 IM集成
查看>>