我在iOS 7项目中为导航栏设置了2个不同的背景图像,具体取决于设备的方向。该代码基于Apple的以下示例......
https://developer.apple.com/library/ios/samplecode/NavBar/Introduction/Intro.html
现在我已更新到iOS 8,并且不再加载横向图像。以下Apple页面告诉我UIBarMetricsLandscapePhone已被弃用......
有没有人知道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
由于
答案 0 :(得分:4)
我已经能够通过从WWDC“使用UIKit构建自适应应用程序”video中引用的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];
}
答案 1 :(得分:3)
iOS8不遵循横向和纵向方向更改的概念,
willAnimateRotationToInterfaceOrientation:duration:
在iOS8中已弃用,需要使用
viewWillTransitionToSize:withTransitionCoordinator:
要获得更深入的洞察力,您需要观看Apple WWDC 2014视频“使用UIKit构建自适应应用程序”