navigationItem的背景修改方法
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"new-nav-bg.png"];
[image drawInRect:rect];
}
@end
如果该方法好用的话 后面添加左右按钮和设置标题就很简单了
// 设置标题
self.navigationItem.title = @”测试“;
// 设置左边的按钮
UIBarButtonItem *returnButton = [[UIBarButtonItem alloc]initWithTitle:@”返回“style:UIBarButtonItemStyleBordered target:self action:@selector(returnMain)];
self.navigationItem.leftBarButtonItem = returnButton;
[returnButton release];
//定义右边按钮
UIBarButtonItem *diquButtons = [[UIBarButtonItem alloc]initWithTitle:@”编辑“style:UIBarButtonItemStyleBordered target:self action:@selector(selectArea)];
self.navigationItem.rightBarButtonItem = diquButtons;
self.diquButton = diquButtons;
[diquButtons release];
[temp release];
但是如果该方法不是很好用的话 只有用这个方法了
- (void)initNavBarItems:(NSString *)titlename{
// 设置navbar的titleview
UIView *dd = [[UIView alloc]initWithFrame:CGRectMake(-5, 0, 325, 44)];
UIImageView *imgs = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"new-nav-bg.png"]];
imgs.frame = CGRectMake(-5, 0, 325, 44);
[dd addSubview:imgs];
// 设置标题
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(-5, 3, 320, 40)];
title.text = titlename;
title.textAlignment = UITextAlignmentCenter;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
title.font = [UIFont boldSystemFontOfSize:22];
[dd addSubview:title];
// 设置左边的按钮
UIButton *left = [UIButton buttonWithType:UIButtonTypeRoundedRect];
left.frame = CGRectMake(15, 5, 60, 30);
[dd addSubview:left];
}
这样做的话 可以设置下标题 也能修改背景图 稍微有点复杂
如果对于一个比较大的项目的话 最好写一个公共类 然后设置它的navbar 然后让所有的导航视图控制器类都继承这个类 这样的话比较方便