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


第一种:直接在你的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,然后命名你想要的名称


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


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


  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"]];  


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


#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




  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值