【发布时间】: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