2014-09-26 173 views
15

隨着iOS 8的只有iPhone和iPad的大小以及肖像和風景的概念已經改變,因此設置導航欄背景圖像是不一樣的。我目前使用以下代碼:iOS 8 NavigationBar BackgroundImage

UIImage *NavigationPortraitBackground = [[UIImage imageNamed:@"nav-image-portrait"] 
             resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; 

UIImage *NavigationLandscapeBackground = [[UIImage imageNamed:@"nav-image-landscape"] 
              resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; 

[[UINavigationBar appearance] setBackgroundImage:NavigationPortraitBackground forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance] setBackgroundImage:NavigationLandscapeBackground forBarMetrics:UIBarMetricsCompact]; 

酒吧度量部分已被棄用的iOS 8的起動時我的應用程序它只是水平時重複在iPhone 6或6個加上條圖像。我已經看過圖像切片,但我不認爲這是解決方案。

portrait landscape

+0

您的問題已解決@ Chris。 – 2014-09-26 05:51:18

回答

69

我發現溶液的寬度。我需要使用方法resizableImageWithCapInsets:resizingMode:並將resizingMode設置爲UIImageResizingModeStretch,否則圖像仍然會在導航欄中平鋪。

目的-C:

[[UIImage imageNamed:@"nav-image-portrait"] 
             resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch]; 

夫特四分之三:

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "image")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default) 
+0

完美。在所有其他解決方案中,圖像重複。你拯救了我的一天。 – nidhin 2017-02-21 07:00:25

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

使用上述代碼它works.And使用小尺寸的圖像(40*navigarbarheight) 40是圖像

+0

對不起,我更新了代碼,顯示我已經在使用帶有可調整大小的帽子插圖的圖像,但似乎沒有工作。 – Chris 2014-09-28 21:00:52

+0

是的,這是行不通的。圖像仍然平鋪。 – HughHughTeotl 2016-07-19 14:51:59

0

這是在更PRECIS和準確的,以適應所有的屏幕尺寸的樣本代碼。它將幫助

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = screenRect.size.width; 
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"header"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, screenWidth-50, 50)] forBarMetrics:UIBarMetricsDefault];