【问题标题】:UINavigationbar loses set image on rotationUINavigationbar 在旋转时丢失设置的图像
【发布时间】:2012-01-07 14:11:17
【问题描述】:

我有一个以根 UINavigationController 开头的应用程序。然后,此控制器会推送和弹出各种其他 UIViewControllers - 一切正常。

我的应用有一个navigationController.navigationBar.backgroundImage 的自定义图形。 iPhone 平台有一张图片。 iPad 平台有两张图片——一张是纵向的,一张是横向的。 iPad 是旋转的问题平台,iPhone 平台是纵向的。

如果平台是 iPad,我已经在 -(void)willRotateToInterfaceOrientation: 中编写了代码来检测旋转,并将 navigationBar.backgroundImage 设置为正确的方向图形。

在 iPad 5.0 模拟器中 - 运行良好;无论方向如何,在屏幕上旋转 iPad 都会生成正确的导航栏图形。

在设备上 - 它不起作用 - 图形在 iPad 设备启动时正确显示为纵向。当我旋转到横向时,导航栏变为标准的灰色导航栏。当我转回灰色的“棍子”时。

我试过打电话给setNeedsDisplay - 没有变化。

我已尝试设置 navigationBar.tintColor = [UIColor clearColor] - 没有变化。

我检查了代码中图形的文件名是否与实际文件名相同。

轮换代码:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    return YES; }
return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        UIImage *titleImage = [UIImage imageNamed:@"StandardHeaderIpadLandscape"];
        self.navigationController.navigationBar.tintColor = [UIColor clearColor];
        [self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics:UIBarMetricsDefault];
        self.navigationController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"StandardBackgroundiPadLandscape.png"]];
        [self.navigationController.navigationBar setNeedsDisplay];
    } else {
        UIImage *titleImage = [UIImage imageNamed:@"StandardHeaderIpadPortrait"];
         self.navigationController.navigationBar.tintColor = [UIColor clearColor];
        [self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics:UIBarMetricsDefault];
        self.navigationController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"StandardBackgroundiPad.png"]];
        [self.navigationController.navigationBar setNeedsDisplay];
    }
}

}

【问题讨论】:

  • 向我们展示旋转方法。先检查navigationBar 是否为nil。之后,我们可以讨论涉及的其他潜在问题。
  • 谢谢,我已经添加了上面的代码。我用 NSLog 检查了它的存在,它确实存在。
  • 如果您在横向启动应用程序会发生什么?在这种情况下图像会显示吗?
  • 以横向模式启动也会导致灰色条。在横向作品中的 iPad 模拟器中启动。与模拟器相比,设备上必须有一些根本不同的东西 - 但我在他的文档中找不到任何东西(除了文件名必须准确)。

标签: objective-c uinavigationcontroller rotation navigationbar


【解决方案1】:

检查titleImage 是否为nil。我感觉您的图片路径不正确,或者这些图片没有正确复制到 iPad。

【讨论】:

  • 嗨,你是正确的 - 检查 titleImage == nil;确实绊倒了我的 NSLog。现在我只需要弄清楚为什么它实际上是零。事实上,肖像图像在旋转时也报告为零。我没有检查——这似乎太明显了。傲慢!
  • 已解决 - 将两个文件重命名为小写。作品。那是我生命中的两个小时,我永远不会回来。非常感谢 an0。
猜你喜欢
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
  • 1970-01-01
相关资源
最近更新 更多