2013-08-05 59 views
1

我想將圖像插入導航欄。如何將背景圖像應用於導航欄

我的代碼是

UINavigationBar *navBar = self.navigationController.navigationBar; 
UIImage *image = [UIImage imageNamed:@"bodyBg.png"]; 
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 

我附上了電流輸出的屏幕截圖。這是因爲圖像尺寸大嗎?

enter image description here

但我期望的輸出是 enter image description here

+2

嗯,是....你應該減少大小 – IronManGill

+0

不,它不應該是這樣做的原因。 –

+0

我相信這是圖像高度的bcoz。你應該真的縮小尺寸。否則,正如其他人所說,你需要使用該圖像作爲圖案圖像。 –

回答

0
self.navigationController.navigationBar.tintColor = [UIColor clearColor]; 
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:UIBarMetricsDefault]; 

我希望,它將工作在V

0

使用UINavigationBar'ssetBackgroundImage: forBarMetrics:方法:

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

你的UIImage應該是appropriate height and width。例如,iPhone 4的視網膜大小將爲640 * 88的肖像。

編輯:這裏要說的是,如果UIImageheightbigger說150像素高度會顯示很多的高度和我們UINavigationBarheight44 pixel

+0

你的答案中有什麼新東西?他也使用了這種方法。 –

+0

在第二個圖像中,有那些條形按鈕項目正確..如何將這些按鈕設置在所需的位置? – user2640613

0

你可以設置導航欄下同碼圖像使用的顏色,你可以把你想要在導航欄上顯示圖像名稱

​​
0

嘗試一下這個代碼..

if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) 
{ 
    [navBar setBackgroundImage:[UIImage imageNamed:@"Nav.png"] forBarMetrics:UIBarMetricsDefault]; 
} 
else 
{ 
    UIImageView *imageView = (UIImageView *)[navBar viewWithTag:1];//any tag 
    if (imageView == nil) 
    { 
     imageView = [[UIImageView alloc] initWithImage: 
        [UIImage imageNamed:@"Nav.png"]]; 
     [navBar insertSubview:imageView atIndex:0]; 
     [imageView release]; 
    } 
} 

看到我的完整的答案從這個鏈接setting-title-for-uinavigation-bar-in-iphone

+0

在第二個圖像中,有那些條形按鈕項目正確..如何將這些按鈕設置在所需的位置? – user2640613

1

首先,使視網膜顯示2048x88像素導航欄圖像尺寸1024x44像素NAD 。

如果您有UINavigationBar的每個視圖控制器上的相同的圖像,把這個AppDelegate中的方法didFinishLaunchingWithOptions

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav-background.png"] forBarMetrics:UIBarMetricsDefault]; 
// This will remove shadow in iOS6 
     if ([[UINavigationBar class] instancesRespondToSelector:@selector(shadowImage)]) { 
      [[UINavigationBar appearance] setShadowImage:[[[UIImage alloc] init] autorelease]]; 
     } 

同時,我看你需要自定義後退按鈕,也把這個在AppDelegate中:

UIImage *backButtonNormal = [UIImage imageNamed:@"nav-back.png"]; 
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
+0

在第二張圖片中,有那些條形按鈕項目正確..如何將這些按鈕設置在所需的位置? – user2640613

0

支票navigationbar此鏈接,添加背景圖像和第一您的navigationbar 圖像尺寸設置爲navigationbar

check this link here

我希望你這個代碼是有用的。

0
[myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"] 
      forBarMetrics:UIBarMetricsDefault]; 

而且這個link

0

試試這個代碼,它爲我工作:

UIView* navigationBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
UIImage * targetImage = [UIImage imageNamed:@"Header_top.png"]; 
UIGraphicsBeginImageContextWithOptions(navigationBGView.frame.size, NO, 0.f); 
[targetImage drawInRect:CGRectMake(0.f, 0.f, navigationBGView.frame.size.width, navigationBGView.frame.size.height)]; 
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
navigationBGView.backgroundColor = [UIColor colorWithPatternImage:resultImage]; 
[self.navigationController.navigationBar addSubview:navigationBGView]; 
[self.navigationController.navigationBar setTintColor:[UIColor clearColor]];