关于NavigationBar背景图片和颜色的设置
2012-06-30 18:11
477 查看
第一种:直接在你的UIViewController中设置//颜色设置,采用RGB格式UIColor *bgcolor = [UIColorcolorWithRed:255.0f/255.0fgreen:0.0f/255.0fblue: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.navigationBaradduSubview: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.navigationBarsetBackgroundImage:[UIImage imageNamed:@"navigation_bar_bg.png"]];}综合了网上其它一些方法,有些没测试出效果
![](http://my.csdn.net/uploads/201206/30/1341050869_6540.png)
![](http://my.csdn.net/uploads/201206/30/1341050977_7320.png)
![](http://my.csdn.net/uploads/201206/30/1341051167_4117.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
相关文章推荐
- 关于NavigationBar背景图片和颜色的设置
- 设置navigationBar的背景颜色 背景图片
- MFC积累---20111105(关于设置背景图片、背景颜色插入背景音乐)
- qt中关于QWidget的背景颜色和背景图片的设置
- qt中关于QWidget的背景颜色和背景图片的设置
- QT中关于QWidget的背景颜色和背景图片的设置
- qt中关于QWidget的背景颜色和背景图片的设置
- QT中关于QWidget的背景颜色和背景图片的设置
- Qt 中关于QWidget的背景颜色和背景图片的设置
- C# 如何给Word文档设置背景颜色和背景图片
- 《安卓应用开发学习》——关于ImageButton图片背景的设置问题
- 关于设置背景图片的透明度(html+css)
- QWidget的背景颜色和背景图片的设置
- 关于IE6,7设置BODY背景图片无效问题
- 关于Android沉浸式状态栏字体颜色和图片背景自适应变化
- 关于UITableView设置背景颜色无效的问题
- 代码里面设置布局颜色以及背景图片
- 关于改变窗口背景颜色、图片的研究
- Qt中设置widget背景颜色背景图片的注意事项
- 关于将navigationBar的背景设置成透明,并且让navigationItem展示出来