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

查看:239
本文介绍了如何在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];


推荐答案

这是来自链接 @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天全站免登陆