19.导航栏颜色的更改

  • 1.效果图
市场_附近_美食_点餐.jpg

代码:上面导航栏设置为了无色:透明

  • 在两个方法里面写四句代码(思想:给导航栏设置一张对应尺寸的全透明图片即可。 )

    -(void)viewWillAppear:(BOOL)animated
    {
         [super viewWillAppear:animated];
    
         //设置导航栏背景图片为一个空的image,这样就透明了
         [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    
         //去掉透明后导航栏下边的黑边
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
       [super viewWillDisappear:(BOOL)animated];
    
       //如果不想让其他页面的导航栏变为透明
       [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
       [self.navigationController.navigationBar setShadowImage:nil];
    }
    
  • 2.改变导航栏的颜色(把下面的代码下载你要改变的控制器里面)

改变导航栏的颜色
  //视图将要显示之前(这个颜色是导航栏的新的颜色)
  -(void)viewWillAppear:(BOOL)animated
  {
      [super viewWillAppear:animated];

      self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
  }

  //视图已经消失(这个颜色是导航栏的原来的的颜色)
  -(void)viewWillDisappear:(BOOL)animated
  {
      [super viewWillDisappear:(BOOL)animated];

      self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
  }

代码:改变颜色

  • 3.怎么改变导航栏上标题的颜色和大小(2个方法)
  • <1>:(自定义视图的方法)
    就是在导航向上添加一个titleView,可以使用一个label,再设置label的背景颜色透明,字体什么的设置就很简单了。

    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor grayColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor greenColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"新闻";
    self.navigationItem.titleView = titleLabel;
    
  • <2>:(在默认显示的标题中直接修改文件的大小和颜色也是可以的)

     [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor redColor]}];
    

方式二相对于方式一而言更加简单方便

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

相关阅读更多精彩内容

友情链接更多精彩内容