设置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=[[UIImageViewalloc] initWithImage:navBarImage];
[self.navigationController.navigationBarinsertSubview:navigationbaritembackgroundViewatIndex:1];//将背景图片设置在你想要的atIndex处
[self.navigationController.navigationBar adduSubview:navigationbaritembackgroundView];//这条指令将会让背景图片出现在最顶层,如果你开始在navigationBar设置得有其它的ui控件的话,它们将会被覆盖隐藏掉;
上面的图片是直接加进去的,无法调整图片的大小,那么你的图片最好先制作为适合你预期的效果样式
第二种:扩展UINavigationBar
这里我先给链接,http://wsqwsq000.iteye.com/blog/1145172
这个我认为说得是最清楚的
what?不知道怎么扩展,那么看下面
选中UINavigationBar,然后命名你想要的名称
添加完成后,出现两个文件
后然在这个类中进行添加函数,后面的看那个链接
- //CustomNavigationBar.h
- @interface UINavigationBar (UINavigationBarCategory)
- UIImageView *backgroundView;
- - (void)setBackgroundImage:(UIImage*)image;
- - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- @end
- //CustomNavigationBar.m
- @implementation UINavigationBar (UINavigationBarCategory)
- -(void)setBackgroundImage:(UIImage*)image
- {
- if(image == nil)
- {
- [backgroundView removeFromSuperview];
- }
- else
- {
- backgroundView = [[UIImageView alloc] initWithImage:image];
- backgroundView.tag = 1;
- backgroundView.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
- backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [self addSubview:backgroundView];
- [self sendSubviewToBack:backgroundView];
- [backgroundView release];
- }
- }
- //for other views
- - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
- {
- [super insertSubview:view atIndex:index];
- [self sendSubviewToBack:backgroundView];
- }
- @end
- //YourViewController.m
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [self.navigationController.navigationBar
- setBackgroundImage:[UIImage imageNamed:@"navigation_bar_bg.png"]];
- }
综合了网上其它一些方法,有些没测试出效果
- #import <UIKit/UIKit.h>
- @interface UINavigationBar (CustomImage)
- - (void)setBackgroundImage:(UIImage*)image;
- @end
- #import "UINavigationBar+CustomImage.h"
- UIImageView *backgroundView;
- @implementation UINavigationBar (CustomImage)
- - (void)setBackgroundImage:(UIImage*)image
- {
- if(image == nil)
- {
- UIColor *bgcolor = [UIColor colorWithRed:46.0f/255.0f green:87.0f/255.0f blue:29.0f/255.0f alpha:1.0f];
- self.tintColor = bgcolor;
- [backgroundView removeFromSuperview];
- }
- else
- {
- backgroundView = [[UIImageView alloc] initWithImage:image];
- //backgroundView.tag = 1;
- backgroundView.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
- backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [self insertSubview:backgroundView atIndex:1];//这条指令atIndex:1达到了预期的效果
- [backgroundView release];
- }
- }
- /*
- - (void)drawRect:(BOOL)color {
- //颜色填充
- if(color==YES )
- {
- UIColor *bgcolor = [UIColor colorWithRed:46.0f/255.0f green:87.0f/255.0f blue:29.0f/255.0f alpha:1.0f];
- self.tintColor = bgcolor;
- return;
- }
- UIImageView *backgroundView;
- UIImage *navBarImage = [UIImage imageNamed:@"banner1.jpg"];
- backgroundView = [[UIImageView alloc] initWithImage:navBarImage];
- [self insertSubview:backgroundView atIndex:1];//这条指令atIndex:1达到了预期的效果
- [backgroundView release];
- /*
- //第一种:颜色填充
- UIColor *color = [UIColor blueColor];
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
- CGContextFillRect(context, rect);
- self.tintColor = color;
- */
- //第二种:图片填充
- /*
- UIImage *img = [UIImage imageNamed: @"net.png"];
- [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
- */
- /*第三种:
- UIImage *img = [UIImage imageNamed: @"net.png"];
- CGPoint point = {0,0};
- [img drawAtPoint:point];
- */
- //[self addSubview:backgroundView]; 这条指令addSubview:会让背景图显示在最前面,覆盖掉工程里的搜索按钮
- //[self sendSubviewToBack:backgroundView];这条指令加上在这个工程里背景图就无法显示了
- //第四种:加入旋转坐标系代码
- // Drawing code
- /*
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextTranslateCTM(context, 0.0, self.frame.size.height);
- CGContextScaleCTM(context, 1.0, -1.0);
- CGPoint center=self.center;
- CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));
- CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80,self.frame.size.height), cgImage);
- CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320,self.frame.size.height), navBarImage.CGImage);
- CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80,self.frame.size.height), cgImage);
- }
- */
- @end