使用自定义导航条注意:
隐藏系统导航条
self.navigationController.navigationBarHidden =YES;
一、导航条左右按钮是图片形式——图片上无文字
1.Nav.h文件
#import <UIKit/UIKit.h>
@interface Nav : UIView
-(id)initWithFrame:(CGRect)frame andLeftBtnTitle:(NSString*)leftTitle andLeftBtnAction:(SEL)leftAction andRightBtnTitle:(NSString*)rightTitle andRightBtnAction:(SEL)rightAction andTarget:(id)target andTitle:(NSString*)title;
@end
2.Nav.m文件
#import "Nav.h"
@implementation Nav
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
}
returnself;
}
-(id)initWithFrame:(CGRect)frame andLeftBtnTitle:(NSString *)leftTitle andLeftBtnAction:(SEL)leftAction andRightBtnTitle:(NSString *)rightTitle andRightBtnAction:(SEL)rightAction andTarget:(id)target andTitle:(NSString *)title
{
self = [super initWithFrame:frame];
if(self)
{
//背景图片
UIImageView *backImage = [[UIImageView alloc] initWithFrame:self.bounds];
backImage.image = [UIImage imageNamed:@"navigationbar.png"];
[self addSubview:backImage];
[backImage release];
//居中label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.text = title;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor colorWithRed:103/255.0 green:168/255.0 blue:232/255.0 alpha:1];
titleLabel.font = [UIFont boldSystemFontOfSize:22];
[self addSubview:titleLabel];
[titleLabel release];
//左btn
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(20,7, 44,30);
[leftBtn setImage:[UIImage imageNamed:@"buttonbar_action.png"] forState:UIControlStateNormal];
[leftBtn addTarget:target action:leftAction forControlEvents:UIControlEventTouchUpInside];
[self addSubview:leftBtn];
UILabel *leftBtnLabel = [[UILabel alloc] initWithFrame:leftBtn.bounds];
leftBtnLabel.textAlignment = NSTextAlignmentCenter;
leftBtnLabel.text = leftTitle;
[leftBtn addSubview:leftBtnLabel];
[leftBtnLabel release];
leftBtnLabel.userInteractionEnabled = NO;
//右btn
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtn.frame = CGRectMake(self.frame.size.width-64,7, 44,30);
[rightBtn setImage:[UIImage imageNamed:@"buttonbar_action.png"] forState:UIControlStateNormal];
[rightBtn addTarget:target action:rightAction forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightBtn];
UILabel *rightBtnLabel = [[UILabel alloc] initWithFrame:rightBtn.bounds];
rightBtnLabel.textAlignment = NSTextAlignmentCenter;
rightBtnLabel.text = rightTitle;
[rightBtn addSubview:rightBtnLabel];
[rightBtnLabel release];
rightBtnLabel.userInteractionEnabled = NO;
}
returnself;
}
@end
二、导航条左右按钮是图片形式——图片上有文字
1.NavView.h文件
#import <UIKit/UIKit.h>
@interface NavView : UIView
-(id)initWithFrame:(CGRect)rect andTitle:(NSString*)title andLeftBtnImage:(NSArray*)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSArray*)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target;
@end
2.NavView.m文件
#import "NavView.h"
@implementation NavView
//因为这个类是继承于UIView的子类,所以这个类也是一个View,所以在这个类力,self就表示这个类所代表的View
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
}
returnself;
}//UIView的子类的主方法就是init方法
-(id)initWithFrame:(CGRect)rect andTitle:(NSString *)title andLeftBtnImage:(NSArray *)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSArray *)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target
{
self = [super initWithFrame:rect];
if(self)
{
//在这里调用下面的布局方法,把init方法中的参数当做下面布局方法的参数
[self makeUIWithTitle:title andLeftBtnImage:leftImage andleftBtnSelector:leftSelector andRightBtnImage:rightImage andRightBtnSelector:rightSelector andTarget:target];
}
returnself;
}
//下面是布局的方法
-(void)makeUIWithTitle:(NSString*)title andLeftBtnImage:(NSArray*)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSArray*)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target
{
//导航条
UIImageView *navView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320,44)];
[self addSubview:navView];
navView.image = [UIImage imageNamed:@"title_bg.png"];
[navView release];
//title
UILabel *navLabel = [[UILabel alloc] initWithFrame:self.bounds];
navLabel.text = title;//根据传过来的参数赋值
navLabel.textColor = [UIColor whiteColor];
navLabel.textAlignment = NSTextAlignmentCenter;
navLabel.font = [UIFont boldSystemFontOfSize:19];
[self addSubview:navLabel];
[navLabel release];
//左按钮
for (int i=0; i<leftImage.count; i++) {
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(10+i*48,10, 43,24);
[leftBtn setImage:[UIImage imageNamed:leftImage[i]] forState:UIControlStateNormal];
[leftBtn addTarget:target action:leftSelector forControlEvents:UIControlEventTouchUpInside];
if(leftImage)
{
[self addSubview:leftBtn];
}
}
//右按钮
for (int i=0; i<rightImage.count; i++) {
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtn.frame = CGRectMake(self.frame.size.width-35-i*35,7, 30,30);
[rightBtn setImage:[UIImage imageNamed:rightImage[i]] forState:UIControlStateNormal];
[rightBtn addTarget:target action:rightSelector forControlEvents:UIControlEventTouchUpInside];
rightBtn.tag = 2000+i;
if(rightImage)
{
[self addSubview:rightBtn];
}
}
}
@end