3

我有一个应用程序,它以根UINavigationController开头。这个控制器然后推动和弹出其他各种UIViewControllers - 这一切都正常工作。UINavigationbar在旋转时丢失了设置图像

我的应用程序有一个navigationController.navigationBar.backgroundImage的自定义图形。 iPhone平台有一个图像。 iPad平台有两个图像 - 一个用于肖像,一个用于景观。 iPad在旋转时是问题的平台,iPhone平台只是肖像。

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

在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]; 
    } 
} 

}

+0

向我们展示旋转方法。检查'navigationBar'是否为零。之后,我们可以讨论其他潜在的问题。 – an0 2012-01-07 14:17:05

+0

谢谢,我已经添加了上面的代码。我检查了NSLog的存在,它确实存在。 – Skybird 2012-01-07 14:33:50

+0

如果您在横向上启动应用程序,会发生什么情况?那么图像会在那种情况下显示出来吗? – Bek 2012-01-07 15:07:06

回答

1

检查titleImage是否是零。我有一种感觉,你的图像路径不正确,或者这些图像没有正确复制到iPad。

+0

嗨,你现在在检查titleImage == nil;旅行我的NSLog。现在我只需要弄清楚为什么地球上它是零。实际上,肖像图像在旋转时也报告为零。我没有检查 - 看起来太明显了。傲慢! – Skybird 2012-01-07 15:48:23

+0

已解决 - 将这两个文件重命名为小写。作品。这是我一生中的两个小时,我永远不会回来。非常感谢an0。 – Skybird 2012-01-07 15:57:26