2014-09-19 84 views
2

我为iOS 7项目中的导航栏设置了2个不同的背景图像,具体取决于设备的方向。该代码是基于苹果下面的例子...当UIBarMetricsLandscapePhone被弃用时设置导航栏背景图像iOS 8

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

现在我的景观形象不再装载更新到iOS 8。下面的苹果页告诉我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 

感谢

回答

3

iOS8上不遵循的横向和纵向观念的变化,以及

willAnimateRotationToInterfaceOrientation:duration: 

在iOS8上已经过时,需要使用

viewWillTransitionToSize:withTransitionCoordinator: 

要获得更深入的洞察力,您需要观看Apple WWDC 2014视频“使用UIKit构建适应性应用”

here

+0

感谢您指点我正确的方向,我会张贴我的解决方案根据您的帮助莫。 – Niall 2014-09-29 12:40:38

4

我已经能够采取从苹果公司的“自适应照片”例如下面的代码在WWDC“建设适应企业应用套件的UIKit” 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]; 
}