最近作项目碰到修改nav bar的情况,暂时总结为下面的部分:
1.使用xib布局时
//自定义UINavigationBar背景图片
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImageimageNamed:@"top_bg.png"];
[imagedrawInRect:CGRectMake(0,0, self.frame.size.width,self.frame.size.height)];
}
@end
可以使用这段代码改变nav bar的背景颜色,添加这部分以后不需要修改任何其他地方的代码2.使用代码生成的nav bar相关的布局,如
这个只是自定义nav bar的相关元素
self.navigationItem.title =@"*****";
UIBarButtonItem *leftItem = [[UIBarButtonItemalloc] initWithTitle:navTitlestyle:UITabBarSystemItemContacts
target:selfaction:@selector(actionBackToHomeView)];
self.navigationItem.leftBarButtonItem = leftItem;
[leftItemrelease];
设置背景的代码
[nav.navigationBarsetBackgroundImage:[UIImageimageNamed:@"top_bg.png"forBarMetrics:UIBarMetricsDefault]];
3.还有第三种方法,但是我测试后还有问题
UIImage *image = [UIImageimageNamed:@"top_bg.png"];
UIImageView *mTopBackView = [[UIImageViewalloc] initWithImage:image];
self.navigationItem.titleView= mTopBackView;
[mTopBackView release];