如何在 iphone 导航栏上添加背景图片? [英] How to add background image on iphone Navigation bar?
本文介绍了如何在 iphone 导航栏上添加背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为导航栏添加图像背景.
I want to add an image background to my navigation bar.
对吗?
//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];
推荐答案
这是 link @luvieere 提到.将此代码粘贴到上面的 rootview 控制器中@implementation rootviewController
Here's the code from the link @luvieere mentioned.
Paste this code into to the rootview controller just above
@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 开发者库)
As of iOS 5, there is an official way to do this. (see iOS Developer Library)
// 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 及更低版本,否则请保留旧代码以实现向后兼容性.
But still, retain the old code for backward compatibility unless you really want to ditch iOS 4 and below.
这篇关于如何在 iphone 导航栏上添加背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文