iOS导航栏与状态栏基本用法

导航栏

  • 隐藏与显示
//显示导航栏(两种都可,要互相对应)
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
//隐藏导航栏
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
  • 透明度
//YES为透明 NO为不透明
    [self.navigationController.navigationBar setTranslucent:NO];
  • 背景
//设置背景颜色
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
//设置背景图片
    [self.navigationController.navigationBar setBackgroundImage:
     [UIImage imageNamed:@"nv_background.png"] forBarMetrics:UIBarMetricsDefault];
  • 标题和左右按钮
//设置标题颜色大小
    [[UINavigationBar appearance] setTitleTextAttributes:@{ UITextAttributeTextColor :[UIColor whiteColor],UITextAttributeFont : [UIFont boldSystemFontOfSize:18]}];
//设置导航栏左右按钮颜色大小
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{
                                   UITextAttributeTextColor :[UIColor whiteColor],
                                   UITextAttributeFont : [UIFont systemFontOfSize:14]
                                   } forState:UIControlStateNormal];
  • 导航栏下方黑线
  UINavigationBar *navigationBar = self.navigationController.navigationBar;
 //隐藏黑色线条
    [navigationBar setShadowImage:[UIImage new]];
//修改线条颜色
    [navigationBar setShadowImage:[self imageWithColor:[UIColor redColor] andSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 1)]];

//颜色生成图片方法
- (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

状态栏

在plist文件中添加如下键值对Status bar is initially hidden(NO为显示状态栏,YES为隐藏“),View controller-based status bar appearance
iOS导航栏与状态栏基本用法_第1张图片
状态栏设置.png
  • 显示与隐藏
//显示
    [UIApplication sharedApplication].statusBarHidden = NO;
//隐藏
    [UIApplication sharedApplication].statusBarHidden = YES;
  • 颜色修改
//状态栏字体颜色为黑色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
//状态栏字体颜色为白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];

你可能感兴趣的:(iOS导航栏与状态栏基本用法)