关于NavigationBar背景图片和颜色的设置

本文介绍了在iOS中设置NavigationBar背景颜色和图片的两种方法:一是直接在UIViewController中设置颜色和背景图片;二是通过扩展UINavigationBar实现自定义背景图片,并详细解释了代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置navigation  的背景颜色

self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:98.0/255.0 green:165.0/255.0 blue:0/255.0 alpha:1.0];



第一种:直接在你的UIViewController中设置

//颜色设置,采用RGB格式

UIColor *bgcolor = [UIColorcolorWithRed:255.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0f];

self.navigationController.navigationBar.tintColor = bicolor;

//设置navigationBar的背景图片

UIImage *navBarImage = [UIImageimageNamed:@"banner1.jpg"];

navigationbaritembackgroundView=[[UIImageViewallocinitWithImage:navBarImage];

[self.navigationController.navigationBarinsertSubview:navigationbaritembackgroundViewatIndex:1];//将背景图片设置在你想要的atIndex处

[self.navigationController.navigationBar adduSubview:navigationbaritembackgroundView];//这条指令将会让背景图片出现在最顶层,如果你开始在navigationBar设置得有其它的ui控件的话,它们将会被覆盖隐藏掉;


上面的图片是直接加进去的,无法调整图片的大小,那么你的图片最好先制作为适合你预期的效果样式


第二种:扩展UINavigationBar

这里我先给链接,http://wsqwsq000.iteye.com/blog/1145172

这个我认为说得是最清楚的

what?不知道怎么扩展,那么看下面


选中UINavigationBar,然后命名你想要的名称


添加完成后,出现两个文件


后然在这个类中进行添加函数,后面的看那个链接

[plain]  view plain  copy
 print ?
  1.   

  1. //CustomNavigationBar.h  
  2. @interface UINavigationBar (UINavigationBarCategory)  
  3. UIImageView *backgroundView;  
  4. - (void)setBackgroundImage:(UIImage*)image;  
  5. - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;  
  6. @end  
  7.   
  8. //CustomNavigationBar.m  
  9. @implementation UINavigationBar (UINavigationBarCategory)  
  10. -(void)setBackgroundImage:(UIImage*)image  
  11. {  
  12.     if(image == nil)  
  13.     {  
  14.         [backgroundView removeFromSuperview];  
  15.     }  
  16.     else  
  17.     {  
  18.         backgroundView = [[UIImageView alloc] initWithImage:image];  
  19.         backgroundView.tag = 1;  
  20.         backgroundView.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);  
  21.         backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  
  22.         [self addSubview:backgroundView];  
  23.         [self sendSubviewToBack:backgroundView];  
  24.         [backgroundView release];  
  25.     }  
  26. }  
  27.   
  28. //for other views  
  29. - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index  
  30. {  
  31.     [super insertSubview:view atIndex:index];  
  32.     [self sendSubviewToBack:backgroundView];  
  33. }  
  34. @end  
  35.   
  36. //YourViewController.m  
  37. - (void)viewWillAppear:(BOOL)animated  
  38. {  
  39.     [super viewWillAppear:animated];  
  40.     [self.navigationController.navigationBar  
  41.         setBackgroundImage:[UIImage imageNamed:@"navigation_bar_bg.png"]];  


综合了网上其它一些方法,有些没测试出效果

[plain]  view plain  copy
 print ?
  1.   

[plain]  view plain  copy
 print ?
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface UINavigationBar (CustomImage)  
  4. - (void)setBackgroundImage:(UIImage*)image;  
  5. @end  



[plain]  view plain  copy
 print ?
  1. #import "UINavigationBar+CustomImage.h"  
  2. UIImageView *backgroundView;   
  3. @implementation UINavigationBar (CustomImage)  
  4. - (void)setBackgroundImage:(UIImage*)image  
  5. {  
  6.     if(image == nil)    
  7.     {    
  8.         UIColor *bgcolor = [UIColor colorWithRed:46.0f/255.0f green:87.0f/255.0f blue:29.0f/255.0f alpha:1.0f];  
  9.         self.tintColor = bgcolor;  
  10.         [backgroundView removeFromSuperview];    
  11.     }    
  12.     else    
  13.     {    
  14.         backgroundView = [[UIImageView alloc] initWithImage:image];    
  15.         //backgroundView.tag = 1;    
  16.         backgroundView.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);    
  17.         backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;    
  18.         [self insertSubview:backgroundView atIndex:1];//这条指令atIndex:1达到了预期的效果  
  19.         [backgroundView release];    
  20.     }    
  21. }  
  22.   
  23. /*  
  24. - (void)drawRect:(BOOL)color {   
  25.     //颜色填充  
  26.     if(color==YES )  
  27.     {  
  28.         UIColor *bgcolor = [UIColor colorWithRed:46.0f/255.0f green:87.0f/255.0f blue:29.0f/255.0f alpha:1.0f];  
  29.         self.tintColor = bgcolor;  
  30.         return;  
  31.     }  
  32.     UIImageView *backgroundView;   
  33.     UIImage *navBarImage = [UIImage imageNamed:@"banner1.jpg"];  
  34.       
  35.     backgroundView = [[UIImageView alloc] initWithImage:navBarImage];   
  36.     [self insertSubview:backgroundView atIndex:1];//这条指令atIndex:1达到了预期的效果   
  37.     [backgroundView release];  
  38.       
  39.     /*  
  40.     //第一种:颜色填充  
  41.         UIColor *color = [UIColor blueColor];  
  42.         CGContextRef context = UIGraphicsGetCurrentContext();  
  43.         CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));  
  44.         CGContextFillRect(context, rect);  
  45.         self.tintColor = color;  
  46.     */  
  47.       
  48.     //第二种:图片填充  
  49.     /*  
  50.     UIImage *img    = [UIImage imageNamed: @"net.png"];  
  51.     [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];     
  52.      */  
  53.     /*第三种:  
  54.     UIImage *img = [UIImage imageNamed: @"net.png"];  
  55.     CGPoint point = {0,0};  
  56.     [img drawAtPoint:point];  
  57.      */  
  58.       
  59.     //[self addSubview:backgroundView]; 这条指令addSubview:会让背景图显示在最前面,覆盖掉工程里的搜索按钮   
  60.     //[self sendSubviewToBack:backgroundView];这条指令加上在这个工程里背景图就无法显示了  
  61.       
  62.     //第四种:加入旋转坐标系代码  
  63.     // Drawing code  
  64.     /*  
  65.      CGContextRef context = UIGraphicsGetCurrentContext();  
  66.      CGContextTranslateCTM(context, 0.0, self.frame.size.height);  
  67.      CGContextScaleCTM(context, 1.0, -1.0);  
  68.      CGPoint center=self.center;  
  69.        
  70.      CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));  
  71.      CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80,self.frame.size.height), cgImage);  
  72.      CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320,self.frame.size.height), navBarImage.CGImage);  
  73.      CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80,self.frame.size.height), cgImage);  
  74.       
  75. }   
  76.  */  
  77. @end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值