使用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)