2009-11-07 251 views
32

我想爲我的導航條添加圖像背景如何在iphone導航欄上添加背景圖片?

是不是正確?

//set custom background image 
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]]; 
[self.navigationBar insertSubview:backgroundView atIndex:0]; 
[backgroundView release]; 
+0

我更喜歡這個代碼,因爲不推薦使用類別。如果你使用它,你會發現它工作正常。 – Steve

回答

98

這裏是link @luvieere提到的代碼。 略高於 @implementation rootviewController

@implementation UINavigationBar (CustomImage) 
- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed:@"NavigationBar.png"]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 
@end 

隨着iOS 5的這個代碼粘貼到到rootview控制器,還有就是做這個的官方途徑。 (見iOS Developer Library

// someplace where you create the UINavigationController 
if ([navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
    UIImage *image = [UIImage imageNamed:@"NavigationBar.png"]; 
    [navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
} 

但儘管如此,保留向後兼容舊的代碼,除非你真的想拋棄的iOS 4或以下。

+2

不要忘記加入@luvieere,他值得信任。 – iwat

0

您還可以添加一個擴展UINavigationBar類並覆蓋類別中的drawRect:方法的類別。

+0

這樣的代碼是正確的?我還沒有嘗試! :D – Momi

+0

很難說。你會在哪裏使用它?您可能需要在每次顯示酒吧時將背景圖片視圖展示在前面 – nduplessis

32

您的代碼本身不會這樣做,您必須編寫一個類別才能使其工作。關於你應該怎麼做的方法有兩種方法:第一種方法是將圖像作爲UINavigationBar的子視圖,並將其重新放在每個UIViewControllerviewDidAppear方法中。然而,這已被報道與覆蓋右邊UIBarButtonItem有一些問題。另一種方法包括首要

- (void)drawRect:(CGRect)rect

和繪製圖像那裏。這兩個在this blog討論中被廣泛地包括。

9

最簡單的方法就是設置UINavigationBar圖層內容。

NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"mybgimage" ofType:@"png"]; 
[nBar.layer setContents: (id)[UIImage imageWithContentsOfFile: barBgPath].CGImage]; 

的缺點是按鍵也不會產生過正確的色彩,但你可以設置導航欄的顏色最接近什麼是你的背景圖片和色調應採取照顧。

+0

這種方法非常容易,並且可以爲用於繪圖的圖像提供更多的靈活性。我剛剛嘗試過,它效果很好!只要記得導入'QuartzCore'庫。 – FreeAsInBeer

+0

很好,謝謝。我試圖避開類別,我發現它們不可靠,所以這很好。 – Chris

8

我會做這樣的的appdelegate像

+ (void)Generalstyle { 
    //navigationbar 
    UINavigationBar *navigationBar = [UINavigationBar appearance]; 
    [navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation.png"] forBarMetrics:UIBarMetricsDefault]; 

} 

而且在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    [[self class] Generalstyle]; 

} 

.h文件中:

+ (void) Generalstyle; 
2

當iPhone 5的來臨,我們就必須設置兩個設備類型。因此,使用這種

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]; 
} 

欲瞭解更多信息請訪問在iOS5的這個link

8

,我在AppDelegate.m設置導航欄背景圖片(所有導航欄圖像):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [application setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO]; 
    UIImage *navBarBackgroundImage = [UIImage imageNamed:@"nav_bg"]; 
    [[UINavigationBar appearance] setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsDefault]; 
    return YES; 
} 
+1

使用代理外觀的+1 –

0

什麼我做的是剛剛創建了一個UIImage與我想要的圖像,並添加到這樣的導航欄。

UIImage *image = [UIImage imageNamed:@"yourImage.png"]; 

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
2

這一個在IOS 6工作正常和7

UINavigationBar *navBar = [[self navigationController] navigationBar]; 
    UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"]; 
    [navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; 
1

夫特版本

let navBar = UINavigationBar.appearance(); 
navBar.setBackgroundImage(UIImage(named: "yourImageName"), forBarMetrics: .Default) 

更新夫特3.2

let navBar = UINavigationBar.appearance(); 
     navBar.setBackgroundImage(UIImage(named: "yourImageName"), for: .default) 
0

在添加導航欄背景圖片時,您應該非常小心它的尺寸。請確保您擁有不同屏幕尺寸的以下尺寸。

1) background.png =>320x44
2) [email protected] =>640x88 //用於iPhone5的和視網膜設備
3)背景@ 3倍.png =>1334x183 //用於iPhone6

使用以下代碼添加背景圖像並避免在導航欄的背景圖像中出現任何平鋪。

[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault]; 
0

在您的AppDelegate類中嘗試此代碼以在導航欄中顯示圖像。它會幫助你很多。

[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"headerImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];