Iphone 更改每个导航栏上的导航栏背景图像
我正在开发iPhone应用程序。我添加了导航栏背景图像 带接口:-Iphone 更改每个导航栏上的导航栏背景图像,iphone,image,background,navigation,Iphone,Image,Background,Navigation,我正在开发iPhone应用程序。我添加了导航栏背景图像 带接口:- @interface-UINavigationBar(带标题的背景图像) 方法:- -(void)drawRect:(CGRect)rect 通过此方法,导航栏背景图像将一次性设置 我想从不同的.m文件调用它,以便在条形图上指定不同的图像 如何实施 提前感谢。CustomNavigation.h #import <Foundation/Foundation.h> @interface UINavi
@interface-UINavigationBar(带标题的背景图像)
方法:-
-(void)drawRect:(CGRect)rect
通过此方法,导航栏背景图像将一次性设置
我想从不同的.m
文件调用它,以便在条形图上指定不同的图像
如何实施
提前感谢。CustomNavigation.h
#import <Foundation/Foundation.h>
@interface UINavigationBar (UINavigationBarCustomDraw){
}
@end
如果您想在FirstViewController中使用First.png设置navigationBar背景图像,则
在您的FirstViewController.m中
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=@"First";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=@"Second";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
如果您想使用Second.png在SecondViewController中设置navigationBar背景图像,则
在您的SecondViewController.m中
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=@"First";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=@"Second";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
在iOS 5上,这将不起作用,但好消息是,有一种简单的方法可以做到这一点
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault];
注意:这只适用于iOS 5及以上版本,因此如果您想向后兼容,请确保检查iOS版本。我已经尝试过了
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“name”]forBarMetrics:UIBarMetricsDefault]
它适用于iOS 5.0,但在iOS 4.3或更低版本中崩溃
您已经设置了条件,即
iOS 5
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“name”]forBarMetrics:UIBarMetricsDefault]
其他
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“name”]
但仍然有一个问题,它显示在图像下的线
或者你可以这样做
self.navigationItem.hidesBackButton = YES;
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
imgView.image = image;
[tempView addSubview:imgView];
[navBar clearBackgroundImage];
[navBar addSubview:tempView];
[tempView release];
if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
//[[UINavigationBar appearance] setFrame:CGRectMake(0, 0, 320, 40)];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
}
或其他选项,以便代码仅在iOS 5的情况下运行
if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
}
你可以这样试试
self.navigationItem.hidesBackButton = YES;
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
imgView.image = image;
[tempView addSubview:imgView];
[navBar clearBackgroundImage];
[navBar addSubview:tempView];
[tempView release];
if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
//[[UINavigationBar appearance] setFrame:CGRectMake(0, 0, 320, 40)];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
}
在其他情况下,您可以使用(SDK 3.2.5.)添加以前的代码,即setBackgroundImage…,-在我的一个应用程序中,我需要在横向和纵向中使用自定义图像的导航栏
我所做的是:
@implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage *image;
if(self.frame.size.width == 320) //for iPhone - portrait will have 320 pix width.
{
image = [UIImage imageNamed: @"portraitNavigationBar.png"];
}
else
{
image = [UIImage imageNamed: @"landscapeNavigationBar.png"];
}
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
幸运的是,我的应用程序支持旋转-所以,当旋转-导航栏自动重画自己
但要手动重新绘制,您还可以调用:
[navigationController.navigationBar setNeedsDisplay];
感谢您的回复,但它正确地调用了firstviewcontroller,而不是secondviewcontroller。如何第二次调用它?验证第二个png是否存在,并验证first和second的拼写对不起,它不起作用我已经检查了每件事。我是否缺少任何其他设置或链接?因此我是否回答了您的问题?请记住接受您所问问题的答案!:)由于“外观”仅在iOS 5中可用,那么唯一有效的是您发布的最后一个代码块-但该代码块存在一个问题,即任何后退按钮都不起作用,这会破坏导航控制器的功能-知道如何解决这个问题吗?如果([[UINavigationBar class]respondsToSelector:@selector(外观)]//iOS>=5.0{/[[UINavigationBar外观]setFrame:CGRectMake(0,0320,40)];[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“navigationBar.png”]forBarMetrics:UIBarMetricsDefault];[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“navBar scape.png”]forBarMetrics:UIBarMetricsLandscapePhone];}