将图像添加到导航栏

4th*_*ace 31 iphone uinavigationbar uitableview uiimageview iphone-sdk-3.0

我想要一个图像占据所有的导航栏.这是基于导航的应用程序附带的导航.它出现在RootViewController上,附带UITableView.我已经看到了一些如何运作的例子.

设置导航栏标题:

UIImage *image = [UIImage imageNamed:@"TableviewCellLightBlue.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar.topItem setTitleView:imageView];
Run Code Online (Sandbox Code Playgroud)

问题是它只覆盖标题而不是整个导航栏.

还有这个帖子:http://discussions.apple.com/message.jspa?messageID = 9254241#9254241.接近最后,该解决方案看起来使用了一个我不使用的标签栏.设置导航栏背景是否复杂?还有其他一些更简单的技术吗?

我想有一个导航背景,仍然可以使用标题文本.

Jea*_*ser 36

在您的情况下,在另一个答案中找到的此解决方案将很好.

将"CustomImage"类别添加到UINavigationBar后,您只需调用:

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"yourNavBarBackground.png"];
[navBar setBackgroundImage:image];
Run Code Online (Sandbox Code Playgroud)

这段代码应该放在方法中

- (void)viewWillAppear:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

您要拥有自定义图像的视图控制器的视图.而且,在这种情况下你最好打电话:

[navBar clearBackgroundImage]; // Clear any previously added background image
Run Code Online (Sandbox Code Playgroud)

在setBackgroundImage之前(否则会多次添加...)


use*_*790 23

它改为ios6,使其在ios 6中使用:

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)


Cha*_*ani 9

UIImage *image = [UIImage imageNamed:@"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

[self.navigationController.navigationBar addSubview:imageView];
Run Code Online (Sandbox Code Playgroud)


CIF*_*ter 8

实际上有一种更简单的方法可以将背景图像添加到任何UIView类或子类.它不需要类分类或扩展(子类化),您可以在"根据需要"的基础上执行此操作.例如,要将背景图像添加到视图控制器的导航栏,请执行以下操作:

self.navigationController.navigationBar.layer.contents = (id)[UIImage 
    imageNamed:@"background.png"].CGImage;
Run Code Online (Sandbox Code Playgroud)

您需要记住将Quartz Core框架添加到项目中并添加#import <QuartzCore/QuartzCore.h>到需要执行此操作的任何位置.这是一种更简洁,更简单的方法来改变从中继承的任何东西的绘图层UIView.当然,如果你想为所有导航栏或标签栏完成类似的效果,那么子类化是有意义的.