2013-01-02 79 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