Custom UINavigationBar/UIToolBar background image (转帖)

2011-10-15

原文来自: <http://thesdkblog.com/2011/05/custom-uinavigationbaruitoolbar- background-image/> 虽然给 UINavgationBar / UIToolBar 添加背景图有很多方法. 如果,你有多个 toolbar 那么给每个 toolbar 添加背景图会变得很痛苦. 下面将展示怎么在你的 app 中为所有的 toolbar 添加背景图.

@implementation UINavigationBar (Background)
-(void)drawRect:(CGRect)rect {
	[[UIImage imageNamed:@"NavBar.png"] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

@implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect {
    [[UIImage imageNamed: @"NavBar.png"] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end 

注: 以上代码必须添加在你的 AppDelegate.m 的 @end 后面