关于NavigationBar的笔记

1常用几个方法 全局

//设置navigationBar 的类型 ,ps:

status bar的状态受navigationbar控制(当用navigationcontroller时,通过设置此属性改变状态栏的状态)

 
    self.navigationBar.barStyle = UIBarStyleDefault;

//status bar的状态受navigationbar控制

   self.navigationBar.barStyle = UIBarStyleDefault;

//设置背景

   [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

//设置背景图片

[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg"] forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];

//设置背景透明

 [self.navigationBar setTranslucent:true] ;

  [self.navigationBar setBackgroundImage:[UIImage  new] forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];

//返回item或其他item的图片或文字颜色

    [[UINavigationBar appearance] setTintColor:[UIColor redColor]];

//设置标题的属性

    //标题文字属性
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowBlurRadius = 5;//模糊度
    shadow.shadowColor = [UIColor whiteColor];//阴影颜色
    shadow.shadowOffset = CGSizeMake(1, 5);//阴影的大小
    
    [self.navigationBar setTitleTextAttributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:22],
        NSForegroundColorAttributeName:[UIColor greenColor],
        NSShadowAttributeName:shadow
                                                 }];

 //隐藏某个控制器的导航返回按钮

[self.navigationItem setHidesBackButton:true];

  //改变返回的文字的位置(消失)

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000) forBarMetrics:UIBarMetricsDefault];

    //设置返回按钮显示的图片(以下两个属性需同时设置)

    [self.navigationBar setBackIndicatorImage:[UIImage new]];

    [self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];

2 单独设置某个页面的返回按钮的时候

  假设A--->push-->B  要设置B的返回键文字为‘b的返回’

  那么要在A控制器中设置

     self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"b的返回" style:UIBarButtonItemStylePlain target:self action:@selector(pop)]

3 note!!!

若出现下面的需求:及当前的控制器隐藏掉导航栏设置annimated属性为No的话会出现不可预料的bug,最好这里animated:yes 或者和视图的一样!

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

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

   }

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:animated];

}

原文地址:https://www.cnblogs.com/cnman/p/6486922.html