这是来自@luvieere 提到的链接的代码。将此代码粘贴到上面的 rootview 控制器中
@implementation rootviewController
@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
从 iOS 5 开始,有一种官方方法可以做到这一点。(参见iOS 开发者库)
// someplace where you create the UINavigationController
if ([navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
[navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
但是,除非您真的想放弃 iOS 4 及更低版本,否则请保留旧代码以实现向后兼容性。