ios - iOS 8 弃用 UIBarMetricsLandscapePhone 时设置导航栏背景图片

标签 ios iphone uinavigationbar deprecated metrics

我正在为我的 iOS 7 项目中的导航栏设置 2 个不同的背景图像,具体取决于设备的方向。该代码基于 Apple 的以下示例...

https://developer.apple.com/library/ios/samplecode/NavBar/Introduction/Intro.html

现在我已经更新到 iOS 8,横向图像不再加载。以下 Apple 页面告诉我 UIBarMetricsLandscapePhone 已弃用...

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarPositioning_Protocol/index.html#//apple_ref/doc/uid/TP40013381-CH1-SW2

有人知道 iOS 8 的方法吗?一些正常的 developer.apple.com 页面似乎在一天的大部分时间都处于关闭状态。请参阅下面我的代码要点。

@interface cQPMNavigationViewController : UINavigationController
@end  

@implementation cQPMNavigationViewController

 - (void)applyImageBackgroundToTheNavigationBar
{
  UIImage *bgImagePortrait = [UIImage imageNamed:@"portrait_bg.png"];
  UIImage *bgImageLandscape = [UIImage imageNamed:@"landscape_bg.png"];

  bgImagePortrait = [bgImagePortrait resizableImageWithCapInsets:UIEdgeInsetsMake(0,0,bgImagePortrait.size.height - 1,bgImagePortrait.size.width - 1)];
  bgImageLandscape = [bgImageLandscape resizableImageWithCapInsets:UIEdgeInsetsMake(0,0,bgImageLandscape.size.height - 1,bgImageLandscape.size.width - 1)];

  [[UINavigationBar appearance] setBackgroundImage:bgImagePortrait
                                 forBarMetrics:UIBarMetricsDefault];
  [[UINavigationBar appearance] setBackgroundImage:bgImageLandscape
                                 forBarMetrics:UIBarMetricsLandscapePhone];

}

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self applyImageBackgroundToTheNavigationBar];
}
@end

谢谢

最佳答案

我已经能够通过从 WWDC“使用 UIKit 构建自适应应用程序”中引用的 Apple 的“自适应照片”示例中获取以下代码来解决我的问题 video .原始代码与新回调共存于我的 cQPMNavigationViewController 文件中,提供旧版 iOS 支持。

- (void)updateConstraintsForTraitCollection:(UITraitCollection *)collection
{
  UIImage *bgImage;

   if (collection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
    bgImage = [UIImage imageNamed:@"landscape_bg.png"];
  } else {
    bgImage = [UIImage imageNamed:@"portrait_bg.png"];
  }

  bgImage = [bgImage resizableImageWithCapInsets:UIEdgeInsetsMake(0,0,bgImage.size.height - 1,bgImage.size.width - 1)];

  [self.navigationBar setBackgroundImage:bgImage
                           forBarMetrics:UIBarMetricsDefault];
}

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
  [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
    [self updateConstraintsForTraitCollection:newCollection];
    [self.view setNeedsLayout];
  } completion:nil];
}

关于ios - iOS 8 弃用 UIBarMetricsLandscapePhone 时设置导航栏背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25938487/

相关文章:

ios - UITableViewController 显示人员信息 - 需要编辑帮助

ios - AFNetworking+UIImageView 中的缓存不遵守任何 http 缓存策略? (希望我错了)

ios - 从 AppDelegate 内部执行 pushViewController

ios - 启动图像和启动画面之间的区别

iphone - iOS 6 SDK SLRequest 返回 '400'

ios - 当 MPMoviePlayerController 退出全屏时 UINavigationBar 错位(ios 8 问题)

ios - UINavigationBar 设置背景,并设置半透明不起作用

当前位置的 iPhone 蜂窝三角测量

iphone - 通过 FB connect 获取好友事件时出现问题

swift - UINavigationBar 的标题不会随着 push segue 的转换而改变