最近我突然发现UINavigationbar背景修改的方法不起作用了,代码如下:
@implementationUINavigationBar(CustomImage)
-(void)drawRect:(CGRect)rect
{
UIImage*image=[UIImage imageNamed:@"navigationbar.png"];
[image drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
}
@end
发现原来是iOS 5的原因,如果运行在iOS 5以下的版本就没有问题了。经过实验以下方法适合iOS 5(放在ViewDidLoad中):if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"]forBarMetrics:UIBarMetricsDefault];
}
iOS5之前是如何自定义UINavigationBar背景的?
在iOS5.0中我们可以非常简单的设置UINavigationBar的背景(setBackgroundImage: forBarMetrics:方法),而这对于之前的版本是不可同日而语的。通过网络收集整理了一下以前的各种方式,只能作为学习笔记做个记录,菜鸟学习而已。高人就跳过吧。
方法一:主要技巧就是用视图的drawInRect:方法绘制
如下为创建了一个UINavigationBar Category
//其实现代码如下
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
//颜色填充
UIColor *color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context,CGColorGetComponents([color CGColor]));
CGContextFillRect(context,rect);
self.tintColor = color;
//图片填充
UIColor *color = [UIColor colorWithRed:46.0f/255.0fgreen:87.0f/255.0fblue:29.0f/255.0falpha:1.0f];
UIImage *img = [UIImage imageNamed:@"bg.png"];
[img drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
self.tintColor = color;
}
@end
自定义图片背景以下两句代码是关键:
UIImage *img = [UIImage imageNamed:@"bg.png"];
[img drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
或者:
UIImage *img = [UIImage imageNamed:@"bg.png"];
CGPoint point = {0,0};[img drawAtPoint:point];
或者:
//加入旋转坐标系代码
// Drawing code
UIImage*navBarImage = [UIImageimageNamed:@"LOGO_320×44.png"];
CGContextRefcontext =UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context,0.0,self.frame.size.height);
CGContextScaleCTM(context,1.0, -1.0);
CGPointcenter=self.center;
CGImageRefcgImage=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);
扩展UINavigationBar的drawRect方法的这种自定义方法会影响到工程项目中所有的导航条栏。
类似在iOS5.0中,由于UINavigationBar、UIToolBar和UITabBar的实现方式改变,而drawRect:方法不会被调用了,所以就不支持这种通过定义导航条类别的方式来自定义导航条了。除非在这类控件的子类中实现。
//子类可以调用drawRect:方法
@interfaceMyNavigationBar : UINavigationBar
@end
@implementationMyNavigationBar
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
}
@end
方法二:定义UINavigationBar的一个static函数
1+ (UINavigationBar *)createNavigationBarWithBackgroundImage:(UIImage *)backgroundImage title:(NSString *)title {
2UINavigationBar *customNavigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,44)] autorelease];
3UIImageView *navigationBarBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
4[customNavigationBar addSubview:navigationBarBackgroundImageView];
5UINavigationItem *navigationTitle = [[UINavigationItem alloc] initWithTitle:title];
6[customNavigationBar pushNavigationItem:navigationTitle animated:NO];
7[navigationTitle release];
8[navigationBarBackgroundImageView release];
9returncustomNavigationBar;
10}
下面是在需要生成UINavgationBar 的地方添加的代码*ViewController.m:
View Code
1self.navigationController.navigationBar.hidden = YES;
2UIImage *navigationBarBackgroundImage =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"topbar-bg"ofType:@"png"]];
3UINavigationBar *customNavigationBar = [YOUR_Util_Class createNavigationBarWithBackgroundImage:navigationBarBackgroundImage title:nil];
4[self.view addSubview:customNavigationBar];
5
6UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0,0.0,75.0,30.0)];
7if(_backButtonImage) {
8[backButton setImage:_backButtonImage forState:UIControlStateNormal];
9}else{
10[backButton setImage:[UIImage imageNamed:@"btnback.png"] forState:UIControlStateNormal];
11}
12
13[backButton addTarget:self action:@selector(backButtonCliked:) forControlEvents:UIControlEventTouchUpInside];
14UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
15customNavigationBar.topItem.leftBarButtonItem = backBarButton;
16
17[backButton release];
18[backBarButton release];
19
20UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,43,30)];
21UIBarButtonItem *addBarButton = [[UIBarButtonItem alloc] initWithCustomView:addButton];
22if(_isFromFavorites) {
23[addButton setImage:[UIImage imageNamed:@"btn-delete-0.png"] forState:UIControlStateNormal];
24[addButton addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
25}else{
26[addButton setImage:[UIImage imageNamed:@"btn_add.png"] forState:UIControlStateNormal];
27[addButton addTarget:self action:@selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
28}
29customNavigationBar.topItem.rightBarButtonItem = addBarButton;
30[addButton release];
31[addBarButton release];
此代码效果图如下:
这一方法转载自:http://www.cnblogs.com/moshengren/archive/2010/10/18/1855191.html
方法三:也是自定义导航条类别,但是重写setBackgroundImage:方法
CustomNavController.h
1//Created by suruiqiang on 8/3/10.
2//Copyright 2010 __MyCompanyName__. All rights reserved.
3//
4#pragmaonce
5#import
6@interfaceUINavigationBar (UINavigationBarCategory)
7UIImageView *bg;
8-(UINavigationBar*)setBackgroundImage:(UIImage*)image;
9- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
10@end
CustomNavController.m
1#import"CustomerNavBarController.h"
2
3@implementationUINavigationBar (UINavigationBarCategory)
4-(UINavigationBar*)setBackgroundImage:(UIImage*)image
5{
6UINavigationBar *NavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,44)];
7if(image == nil)returnNavBar;
8bg = [[UIImageView alloc]initWithImage:image];
9bg.frame = CGRectMake(0.f,0.f, self.frame.size.width, self.frame.size.height);
10[NavBar addSubview:bg];
11[NavBar sendSubviewToBack:bg];
12[bg release];
13returnNavBar;
14}
15
16- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
17{
18[super insertSubview:view atIndex:index];
19[self sendSubviewToBack:bg];
20}
21@end
调用代码示例:
- (void)viewDidLoad {
[super viewDidLoad];
[[self.navigationController navigationBar] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground.png"]];
//在下面添加你自己的功能代码
***********
}
此方法转载自:http://www.cnblogs.com/moshengren/archive/2010/10/18/1855202.html
方法四:通过导入QuartzCore框架绘制CALayer层来自定义
#import
@interfaceDDNavigationViewController : UINavigationController {
CALayer *_barBackLayer;
}
@end
View Code
@implementationDDNavigationViewController
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
self.delegate= self;
returnself;
}
- (void)loadView {
[super loadView];
UINavigationBar *bar = self.navigationBar;
CALayer*layer = [CALayer layer];
UIImage *navBarImage = [UIImage imageNamed:@"navigationBarBackground.png"];
layer.contents = (id)navBarImage.CGImage;
layer.frame= CGRectMake(0,0,320, navBarImage.size.height);
[bar.layer insertSublayer:layer atIndex:0];
_barBackLayer = layer;
}
#pragmamark -
#pragmamark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[_barBackLayer removeFromSuperlayer];
[navigationController.navigationBar.layer insertSublayer:_barBackLayeratIndex:0];
}
@end
# via@新浪微博:王星凯SoWhat
网友评论