IOS 自定义导航栏背景

时间:2023-03-08 16:52:52
//- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
-(id)init
{
self=[super init];
if(self)
{
UINavigationBar *navBar=[self navigationBar];
if([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
//如果是IOS5
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
}
else
{
UIImageView *imgView=(UIImageView*)[navBar viewWithTag:NAVBARIMAGETAG];
if(imgView==nil)
{
imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,navBar.frame.size.width, navBar.frame.size.height)];
[imgView setImage:[UIImage imageNamed:@"NavBar.png"]];
[navBar insertSubview:imgView atIndex:0];
[imgView release];
}
}
}
return self;
}