96.iOS各种 bar 隐藏的方法

来源:互联网 发布:Round it还是around it 编辑:程序博客网 时间:2024/05/20 02:53

1.状态条Status Bar

状态栏隐藏:

[UIApplication sharedApplication].statusBarHidden = YES;//或者[application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];

statusBarHidden属性支持在iOS2.0+, setStatusBarHidden:animated:方法在iOS3.2中开始取消了,而采用了setStatusBarHidden:withAnimation:方法。
上述方法只能实现在程序跳过启动画面的时候才能隐藏状态栏。如果想要在启动画面开始即隐藏状态栏,则要修改app的info.plist文件,新增UIStatusBarHidden键(Status bar is initially hidden),其值是YES。

状态栏风格: 对于状态栏的颜色改变,也要分别从两处着手,代码[[UIApplicationsharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];仅仅改变了启动画面之后的视图上的状态栏,要让App应用在启动画面之时就改变默认颜色,则要修改info.plist文件,新增UIStatusBarStyle键(Status bar style),其值有Opaque black style、Transparent black style和默认的Gray style。

iOS7.0之后, 在需要改变的 controller 中重写下面两个方法, 一个是状态栏隐藏一个是状态栏风格的.

- (BOOL)prefersStatusBarHidden{    return YES;}- (UIStatusBarStyle)preferredStatusBarStyle{    return UIStatusBarStyleLightContent;}

需要刷新时

//self UIViewController实例 [self setNeedsStatusBarAppearanceUpdate];

2.导航条Navigation Bar

[self.navigationController setNavigationBarHidden:YES];//[self.navigationController setNavigationBarHidden:YES animated:YES];

或者

//导航栏透明self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

3.导航栏navigationItem之backButton和自定义 自定义barButtonItem隐藏

//backButton隐藏[self.navigationItem setHidesBackButton:YES];//自定义barButtonItem隐藏self.navigationItem.leftBarButtonItem.customView.hidden = YES;

4.tabbar 隐藏方法

方法一:

[self.tabBarController.tabBar setHidden:YES];

或者在 push 出需要隐藏 tabbar 的 controller 之前, 将 controller 的hidesBottomBarWhenPushed设置为 YES

VC.hidesBottomBarWhenPushed = YES;

方法二:
对于navigationController+tabBarController的结构,可以在push下一级的childController之前将childController的hidesBottomBarWhenPushed属性设为YES。比如,可以在childController的初始化方法中做这件事,代码如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  {       self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];       if (self) {          // Custom initialization.         self.hidesBottomBarWhenPushed = YES;     }  return self; }

方法三:
在 VC中自定义 tabbar 隐藏方法, 或者在 tabBarController 中添加此方法

- (void)makeTabBarHidden:(BOOL)hide {     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];     }     //    [UIView beginAnimations:@"TabbarHide" context:nil];     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;     //    [UIView commitAnimations];     }

方法四:

这个方法是最简单也是最有效的方案, 在你需要隐藏tabbar的控制器中重写下面的hidesBottomBarWhenPushed方法.

- (BOOL) hidesBottomBarWhenPushed{    return (self.navigationController.topViewController == self);}

这个方法的好处在:
它解决了方法一和方法二中的一些问题, 如果你在下一层VC再次push出一个需要显示tabbar的VC时, 不会起作用. 因为hidesBottomBarWhenPushed属性官方文档中有说明

If YES, the bottom bar remains hidden until the view controller is popped from the stack.

所以, 再次push出VC时, 是不会显示tabbar的. 但如果使用方法四, 此问题可以得到解决.

具体可以参考我的另一篇文章hidesBottombarWhenPushed多级push不显示tabbar

1 0
原创粉丝点击