我正在为我的 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
谢谢
最佳答案
我已经能够通过从 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/