iOS 8 导航栏背景图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26052454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 8 NavigationBar BackgroundImage
提问by Chris
With iOS 8 the concept of just iPhone and iPad sizes along with portrait and landscape have changed and therefor setting the navigation bars background image isn't working the same. Currently i'm using the following code:
在 iOS 8 中,iPhone 和 iPad 尺寸以及纵向和横向的概念发生了变化,因此设置导航栏背景图像的工作方式不同。目前我正在使用以下代码:
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];
The bar metrics portion has been deprecated as of iOS 8. When starting up my app it simply repeats the bar image horizontally when on an iPhone 6 or 6 Plus. I've looked into image slices but i don't think thats the solution either.
自 iOS 8 起不推荐使用条形指标部分。在启动我的应用程序时,它只是在 iPhone 6 或 6 Plus 上水平重复条形图像。我已经研究过图像切片,但我认为这也不是解决方案。
回答by Chris
I found the solution. I needed to use the method resizableImageWithCapInsets:resizingMode: and set the resizingMode to UIImageResizingModeStretch, otherwise the image would still tile in the navigation bar.
我找到了解决方案。我需要使用 resizableImageWithCapInsets:resizingMode: 方法并将 resizingMode 设置为 UIImageResizingModeStretch,否则图像仍会在导航栏中平铺。
Objective-C:
目标-C:
[[UIImage imageNamed:@"nav-image-portrait"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
Swift 3 / 4:
斯威夫特 3 / 4:
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "image")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
回答by Suresh Thoutam
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"navbarimg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];
Use the above code it works.And use small size image (40*navigarbarheight)
40 is the width of the image
使用上面的代码就可以了。使用小尺寸图片(40*navigarbarheight)
40是图片的宽度
回答by Shahzaib Maqbool
THis is sample code at more precis and accurate to fit on all the screen sizes . it will help
这是更精确和准确以适合所有屏幕尺寸的示例代码。我会帮你的
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];