【原】iOS学习之UITabBar的隐藏
2024-08-31 02:27:04
当页面使用 UITabBarController + UINavigationController 框架的时候,当跳转到详情页面的时候,如果 UITabBar 仍然存在的话就会造成逻辑混乱,用户体验也会下降,因此我们就有一个在详情页将 UITabBar 隐藏的需求,当然,在其他的一些情况也可能有隐藏 UITabBar 的需求, 在这里小编为大家介绍三种隐藏 UITabBar 的方法,大家可以根据详细的需求进行选择。
1、第一种:
直接隐藏当前页面的 UITabBar
// 显示tabBar
self.tabBarController.tabBar.hidden = NO;
// 隐藏tabBar
self.tabBarController.tabBar.hidden = YES;
2、第二种:
将 push 到的页面的 UItabBar 隐藏
// 在push跳转时隐藏tabBar
UIViewController *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:]; 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 - , 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 - )]; }
}
}
}
[UIView commitAnimations]; }
代码2:
-(void)makeTabBarHidden:(BOOL)hide { // Custom code to hide TabBar
if ( [self.tabBarController.view.subviews count] < )
{
return;
}
UIView *contentView; if ( [[self.tabBarController.view.subviews objectAtIndex:] isKindOfClass:[UITabBar class]] )
{
contentView = [self.tabBarController.view.subviews objectAtIndex:];
} else {
contentView = [self.tabBarController.view.subviews objectAtIndex:];
}
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;
}
以上是小编总结的三种方法,也是从各位大神的博客总结的,如果有什么新的方法,欢迎一起讨论!
最新文章
- B-index、bitmap-index、text-index使用场景详解
- Leetcode 270. Closest Binary Search Tree Value
- ural 1251. Cemetery Manager
- Android课程---Android 如何用滑杆(SeekBar)组件设置图片颜色的透明度(转)
- 模仿$.Callbacks实现
- js节点问题
- OSGI.NET 插件无法启动之情景一
- hdu 1005 java(System.out.println();与System.out.println(“\n”);)
- bzoj2466: [中山市选2009]树
- 《深入Java虚拟机学习笔记》- 第18章 finally子句
- 移动端页面SEO优化需要注意的10个要点
- jQuery.fn和jQuery.prototype jquery.extend() jquery.fn.extend()区别介绍
- python对web服务器做压力测试并做出图形直观显示
- 一个只能用在Win下的密码验证函数(显示星号,可删除)
- MongoDB基础教程系列--第一篇 进入MongoDB世界
- 11-20 bom 浏览器对象模型
- CRM 2013 批量更新two options的缺省值
- Spring Cloud(Dalston.SR5)--Hystrix 断路器
- hive,分桶,内外部表,分区
- JAVA字符串格式化-String.format()的使用【转】