我想在导航栏中添加图像背景.
这样对吗?
//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];
Run Code Online (Sandbox Code Playgroud) 我一直在尝试更改我的应用程序的UINavigationBar的背景图像.我尝试了几种方法.首先,我在AppDelegate类中添加了以下代码:
@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
Run Code Online (Sandbox Code Playgroud)
但它没有用.我的下一个尝试是编写一个覆盖drawRect方法的CustomizedNavigationBar类.它看起来像那样:
CustomizedNavigationBar.h
#import <UIKit/UIKit.h>
@interface CustomizedNavigationBar : UINavigationBar
@end
Run Code Online (Sandbox Code Playgroud)
CustomizedNavigationBar.m
#import "CustomizedNavigationBar.h"
@implementation CustomizedNavigationBar
- (void) drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"navigationbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
NSLog(@"I got called!!!!!");
}
@end
Run Code Online (Sandbox Code Playgroud)
在我定义NavigationBar的.xib文件中,我将类更改为新的CustomizedNavigationBar.但它仍然无法正常工作..
作为另一个测试,我下载了一个示例项目,其中应该更改背景图像.但即使使用该示例代码也无法正常工作.
我究竟做错了什么?我正在使用IOS 5.我可以定义背景图像的任何建议或其他方式吗?
谢谢你的回答!