iOS 8 NavigationBar BackgroundImage [英] iOS 8 NavigationBar BackgroundImage

查看:261
本文介绍了iOS 8 NavigationBar BackgroundImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于iOS 8,只有iPhone和iPad尺寸以及纵向和横向的概念已经改变,因此设置导航栏背景图像的工作方式不同。目前我正在使用以下代码:

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:

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 Plus上水平条形图像。我已经研究过图像切片,但我认为这也不是解决方案。

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.


推荐答案

我找到了解决方案。我需要使用方法resizableImageWithCapInsets:resizingMode:并将resizingMode设置为UIImageResizingModeStretch,否则图像仍会在导航栏中平铺。

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.

Objective-C:

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

Swift 3/4:

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

这篇关于iOS 8 NavigationBar BackgroundImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆