隐藏TabBar的一些方法小结(适用与各种情况)

时间:2023-04-17 08:56:08
  1. 在项目中经常遇到隐藏tabBar,实力很多种方法,可以解决不同情况下问题
  2. 使用中涉及到view的层次关系,下面的使用方法 1、2不做说明;在使用3、4方法时注意要在tabBar所在的rootView中调用实现(必要时使用委托,已达到所需要的目的)
  3. 举例:A(rootView 是tabBarCtroller);B(A的subView);C(B通过pushViewController)
  4. 如果想要C出现的时候将tabView隐藏(且C是全屏的,能展开到tabbar存在的位置),B显示的时候babView在显示出来
  5. 此情况明显1、2方法不能实现了,要用3、4的方法来实现;
  6. 实现方式:B在pushViewController的时候调用其委托函数(即B消失C出现时tabbar隐藏)
  7. if([delegaterespondsToSelector:@selector(hidenTabbar:)])
  8. {
  9. [delegatehidenTabbar:YES];
  10. }
  11. 在A中实现B的委托代码就是3、4;
  12. 同样在B的viewWillAppear中也调用其委托:NO;(B显示时tabbar出现)
  13. -(void)viewWillAppear:(BOOL)animated
  14. {
  15. if([delegate respondsToSelector:@selector(hidenTabbar:)])
  16. {
  17. [delegatehidenTabbar:NO];
  18. }
  19. }
  20. 1://隐藏tabBar
  21. WebViewController *webVc = [[WebViewController alloc] init];
  22. webVc.hidesBottomBarWhenPushed = YES;
  23. [self.navigationController pushViewController:webVc animated:YES];
  24. webVc.hidesBottomBarWhenPushed = NO;
  25. [webVc release];
  26. 2.系统方法    self.hidesBottomBarWhenPushed = YES;
  27. 3:自定义tabBar时候,由tabBarController管理的
  28. //隐藏tabBar
  29. - (void) hideTabBar:(BOOL) hidden{
  30. [UIView beginAnimations:nil context:NULL];
  31. [UIView setAnimationDuration:0];
  32. for(UIView *view in self.tabBarController.view.subviews)
  33. {
  34. if([view isKindOfClass:[UITabBar class]])
  35. {
  36. if (hidden) {
  37. [view setFrame:CGRectMake(view.frame.origin.x, iphone5?568:480, view.frame.size.width, view.frame.size.height)];
  38. } else {
  39. [view setFrame:CGRectMake(view.frame.origin.x, iphone5?568-49:480-49, view.frame.size.width, view.frame.size.height)];
  40. }
  41. }
  42. else
  43. {
  44. if (hidden) {
  45. [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, iphone5?568:480)];
  46. } else {
  47. [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width,  iphone5?568-49:480-49)];
  48. }
  49. }
  50. }
  51. [UIView commitAnimations];
  52. }
  53. //调整子视图
  54. for (UIView *subView in self.view.subviews) {
  55. if ([subView isKindOfClass:NSClassFromString(@"UITransitionView")]) {
  56. //调整子视图的高度,UITransitionView视图为UINavitaionController的根视图
  57. //            subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y, subView.frame.size.width, 480);
  58. CGRect frame = subView.frame;
  59. frame.size.height = 480;
  60. subView.frame = frame;
  61. }
  62. }
  63. 4:类似方法3
  64. - (void)makeTabBarHidden:(BOOL)hide
  65. {
  66. if ( [self.tabBarController.view.subviews count] < 2 )
  67. {
  68. return;
  69. }
  70. UIView *contentView;
  71. if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
  72. {
  73. contentView = [self.tabBarController.view.subviews objectAtIndex:1];
  74. }
  75. else
  76. {
  77. contentView = [self.tabBarController.view.subviews objectAtIndex:0];
  78. }
  79. //    [UIView beginAnimations:@"TabbarHide" context:nil];
  80. if ( hide )
  81. {
  82. contentView.frame = self.tabBarController.view.bounds;
  83. }
  84. else
  85. {
  86. contentView.frame = CGRectMake(self.tabBarController.view.bounds.origin.x,
  87. self.tabBarController.view.bounds.origin.y,
  88. self.tabBarController.view.bounds.size.width,
  89. self.tabBarController.view.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
  90. }
  91. self.tabBarController.tabBar.hidden = hide;
  92. }