如何iphone导航栏上添加背景图片? [英] How to add background image on iphone Navigation bar?

查看:210
本文介绍了如何iphone导航栏上添加背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加图像的背景下,我这样的导航栏:

什么是code?

感谢您

是不是?

  //设置自定义背景图片
*的UIImageView = backgroundView [[UIImageView的页头] initWithImage:[UIImage的imageNamed:@NavigationBackground.png]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView发布]


解决方案

下面是从链接code @luvieere提及。
这code粘贴到到rootview控制器的正上方
@implementation RootViewController的

  @implementation UINavigationBar的(CustomImage)
- (无效)的drawRect:(的CGRect){RECT
    * UIImage的图像= [UIImage的imageNamed:@NavigationBar.png];
    [图像drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
}
@结束

随着iOS 5的,有做这个的官方途径。 (见<一href=\"http://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html#//apple_ref/occ/instm/UINavigationBar/setBackgroundImage%3aforBarMetrics%3a\">iOS开发人员库)

在这里创建的UINavigationController  //某处
如果([navigationController.navigationBar respondsToSelector:@selector(了setBackgroundImage:forBarMetrics :)]){
    * UIImage的图像= [UIImage的imageNamed:@NavigationBar.png];
    [导航栏了setBackgroundImage:图像forBarMetrics:UIBarMetricsDefault];
}

但尽管如此,保留旧code为向后兼容,除非你真的想在下面沟的iOS 4和。

I want to add an image background, to my navigation bar like this :

What is the code ?

Thank you

Is it right ?

//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];

解决方案

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

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];
}

But still, retain the old code for backward compatibility unless you really want to ditch iOS 4 and below.

这篇关于如何iphone导航栏上添加背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆