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

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

问题描述

我要在我的导航栏中添加图片背景,如下所示:

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

代码是什么?

谢谢

是不是?

//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屋!

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