2013-01-02 81 views
1

我想为iOS 6实现自定义的UINavigationBar图像背景,但我没有运气。它显示的是苹果公司的正常版本(没有后退按钮)。 我曾尝试下面的代码内viewDidLoad中(一个在时间):自定义UINavigationBar图像

[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]; 

[self.navigationController.navigationBar setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]; 

我通过呈现所需的视图:

optionViewController *choose= [[optionViewController alloc] initWithNibName:@"optionViewController" bundle:nil]; 
UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:choose]; 
[self presentViewController: aNavController animated:YES completion:nil]; 

任何帮助,将不胜感激。谢谢!

+0

发生什么事? [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@“barImage.png”] forBarMetrics:UIBarStyleDefault];此命令在我的代码中无处不在iOS5和iOS6中。 –

+0

@LêQuýSang没有任何反应......它只是显示典型的基本UINaviationBar而不是我想要的图像。谢谢 –

回答

2

你可以设置导航栏自定义图像像这样: -

 optionViewController *choose= [[optionViewController alloc] initWithNibName:@"optionViewController" bundle:nil]; 
     UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:choose]; 
     UIImage *image = [UIImage imageNamed:@"imageName"]; 
     [aNavController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 

     [self presentModalViewController:aNavController animated:YES]; //change here to PresentViewcontroller to presentmodelViewcontroller 

这里”是图像: -

enter image description here

,并在上面的图像Back按钮不是默认的一个,但它正在通过编程方式。

+0

感谢您的答复,但如果你看上面,我已经尝试过。还有其他建议吗?谢谢 –

+0

你tryong,但你不尝试这一行UIImage * image = [UIImage imageNamed:@“navBarLoc.png”];只是试着这 –

+0

啊对不起,我认为这是理解......我在我的实际项目中的代码行。谢谢 –

1

试试这个,

if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
    //iOS 5 new UINavigationBar custom background 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault]; 
} else { 
    [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0]; 
} 

和对于信息转到这个link