繁体   English   中英

不推荐使用UIBarMetricsLandscapePhone时设置导航栏背景图像iOS 8

[英]Setting Navigation bar background image when UIBarMetricsLandscapePhone is deprecated iOS 8

我在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的“自适应照片”示例中获取以下代码来解决我的问题。 原始代码与我的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];
}

iOS8不遵循横向和纵向方向更改的概念,并且

willAnimateRotationToInterfaceOrientation:duration: 

在iOS8中已弃用,需要使用

viewWillTransitionToSize:withTransitionCoordinator: 

要获得更深入的了解,您需要观看Apple WWDC 2014视频“使用UIKit构建自适应应用程序”

这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM