iOS 8 NavigationBar BackgroundImage

前端之家收集整理的这篇文章主要介绍了iOS 8 NavigationBar BackgroundImage前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用iOS 8,只有iPhone和iPad尺寸以及纵向和横向的概念已经改变,因此设置导航栏背景图像的工作不一样.目前我正在使用以下代码
UIImage *NavigationPortraitBackground = [[UIImage imageNamed:@"nav-image-portrait"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(0,0)];

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

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

酒吧度量部分已经从iOS 8开始弃用.当启动我的应用程序时,只需在iPhone 6或6 Plus上水平重复条形图像.我已经研究了图像切片,但我也不认为是解决方案.

解决方法

我找到了解决方案.我需要使用resizableImageWithCapInsets:resizingMode的方法:将resizingMode设置为UIImageResizingModeStretch,否则图像仍然会在导航栏中显示.

Objective-C的:

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

Swift 2:

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "image")!.resizableImageWithCapInsets(UIEdgeInsetsMake(0,0),resizingMode: .Stretch),forBarMetrics: .Default)

Swift 3:

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

猜你在找的iOS相关文章