转载 http://hua397.iteye.com/blog/1181299
1.背景色:
(http://stackoverflow.com/questions/2259929/iphone-navigationbar-custom-background)网上说用category给UINavigationBar重写drawRect:
@implementation UINavigationBar (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
可以用,他自己也说其实不是很好,我觉得不是很灵活
忘了在哪看到了,说是tintColor,经过测试确实如此,setBackgroundColor不行,tintColor就可以:
UINavigationBar *bar = self.navigationController.navigationBar;
bar.tintColor=[UIColor redColor];
而且连left/right button一起改了(前提是item用tittle初始化):
UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithTitle:@"ad1.png" style:UIBarButtonItemStyleBordered target:nilaction:nil];
self.navigationItem.rightBarButtonItem=item;
效果:
注意:用Image初始化的item加上去有点问题,会覆盖大部分bar,不解:
UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ad1.png"]style:UIBarButtonItemStyleBordered target:nil action:nil];
item.width=50;
self.navigationItem.rightBarButtonItem=item;
效果:
另,item用customview初始化的话,如用button,需要设置frame,不然不显示:
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(0, 0, 50, 30);
[btn setBackgroundImage:[UIImage imageNamed:@"ad1.png"] forState:UIControlStateNormal];
UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem=item;
效果:
2.不止改背景颜色的话,就得用imageView,貌似以前看过,反正就是insert到bar的subviews的0,要不add之后exchange一下,bar原先只有一个subview:
UIImageView *imageView=[[UIImageView alloc] initWithFrame:bar.bounds];
imageView.image=[UIImage imageNamed:@"adBg.png"];
[bar insertSubview:imageView atIndex:0];
[imageView release];
效果: