2013-04-03 120 views
1

我得到了這個形象,我想在我的導航控制器使用方法:製作圖像配合UINavigationBar的酒吧

enter image description here

我設置像這樣(以下建議從this問題):

UINavigationBar *theBar = self.navigationController.navigationBar; 
if ([theBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
    UIImage *image = [UIImage imageNamed:@"cabecalho.jpg"]; 
    [theBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
} 

但是當我運行我的應用程序,我得到這個:

enter image description here

我該怎麼做才能讓我的圖像適合導航控制器?

謝謝。

+0

我有同樣的問題,基本上創建的圖像是與導航欄相同的寬度 - 這不是你的選擇嗎? –

+0

看起來您的圖片尺寸小於導航欄的寬度320,如果圖片很小,它會在背景中重複,請確保您的圖片寬度爲320 px,或者對於Retina爲640 px .. – iphonic

+0

此圖片爲320x49。它應該適合,對吧? – gabsferreira

回答

0

我發現下面的例子this問題的解決方案。

當我創建我的UINavigationController,我這樣做:

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewLista]; 

if ([nav.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
    UIImage *image = [UIImage imageNamed:@"cabecalho.png"]; 
    [nav.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
} 

現在它工作正常。

+0

你的代碼只能工作> = iOS5.0 ..它可以,如果你不支持iOS <5.0,否則你需要改變你的代碼。 – iphonic

+0

對,我現在使用的是iOS6。 – gabsferreira

2

嘗試用下面的代碼

[self.navigationBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]]]; 

參考How to add background image on iphone Navigation bar?這個問題,可能對你有所幫助。

+0

我將此代碼添加到包含導航控制器的視圖的viewWillAppear方法中,但現在它完全不顯示圖像。 – gabsferreira

+0

當然,你必須改變代碼,以便它引用*你的*圖像。 – matt

+0

我做到了,但沒有奏效。我找到了一個解決方案,並將它發佈在這裏。謝謝。 – gabsferreira