ios 单页面修改导航栏

反正啊,就会碰到这样的需求:要把某个页面的导航栏隐藏掉或者修改啊!

How to change it? 新建一个View 去替换

   UIView *statusBarView = [[UIView alloc]   initWithFrame:CGRectMake(0,0,  self.view.bounds.size.width, 50)];
   UILabel *errorMsgLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,  self.view.bounds.size.width, 50)];
   errorMsgLabel.textAlignment = NSTextAlignmentCenter;
   errorMsgLabel.textColor = [UIColor whiteColor];
   errorMsgLabel.text = @"密码错误";
   [statusBarView addSubview:errorMsgLabel];
   statusBarView.backgroundColor =  [UIColor colorWithRed:253/255.0 green:172/255.0 blue:5/255.0 alpha:1/1.0];
   statusBarView.layer.position = CGPointMake(0, 0);
   statusBarView.layer.anchorPoint = CGPointMake(0,0);
   [self.navigationController.navigationBar addSubview:statusBarView];

How to animate it?

   UIView *tipView = [[UIView alloc]   initWithFrame:CGRectMake(0,0,  self.view.bounds.size.width, 0)];
   UILabel *errorMsgLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,  self.view.bounds.size.width, 50)];
   errorMsgLabel.textAlignment = NSTextAlignmentCenter;
   errorMsgLabel.textColor = [UIColor whiteColor];
   [tipView addSubview:errorMsgLabel];
   tipView.backgroundColor =  [UIColor colorWithRed:253/255.0 green:172/255.0 blue:5/255.0 alpha:1/1.0];
   [self.navigationController.navigationBar addSubview:tipView];

   [UIView animateWithDuration:1.0 animations:^{
       errorMsgLabel.text = [error errorMessage];
       [tipView setFrame:CGRectMake(0,0,  self.view.bounds.size.width, 50)];
   }completion:^(BOOL complete) {
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [tipView setFrame:CGRectMake(0,0,  self.view.bounds.size.width, 0)];
       });
   }];

How to hide it?

@property (assign) BOOL ifHidden;
 
self.ifHidden = YES;
 if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
         [self prefersStatusBarHidden];
         [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
 }

-(BOOL)prefersStatusBarHidden
{
    return self.ifHidden;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

如果是想把整个应用程序的状态栏都隐藏掉呢?

一、View controller-based status bar appearance 设置为 NO

1、plist
在plist里增加一行 UIStatusBarStyle,选择UIStatusBarStyleDefault 或者UIStatusBarStyleLightContent
or
在info.plist上添加一项:Status bar is initially hidden,value为YES;

注意:info.plist文件中,View controller-based status bar appearance项设为YES,则View controller对status bar的设置优先级高于application的设置。为NO则以application的设置为准,view controller的prefersStatusBarHidden方法无效,是根本不会被调用的。

2、代码设置

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
二、View controller-based status bar appearance 设置为 YES

1、代码

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;//UIStatusBarStyleDefault 黑
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容