UINavigationbar 在旋转时丢失设置图像
我有一个以根 UINavigationController
开头的应用程序。然后,该控制器会推送和弹出各种其他 UIViewControllers - 一切正常。
我的应用程序有一个 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];
}
}
}
I have an app which starts with a root UINavigationController
. This controller then pushes and pops various other UIViewControllers
- that all works fine.
My app has a custom graphic for the navigationController.navigationBar.backgroundImage
. There is one image for the iPhone platform. There are two images for the iPad platform - one for portrait, one for landscape. The iPad is the platform of issue as it rotates, the iPhone platform is portrait only.
I had already written code in -(void)willRotateToInterfaceOrientation:
to detect the rotation, and set the navigationBar.backgroundImage
to the correct orientation graphic, if the platform is iPad.
In the iPad 5.0 simulator - it works fine; rotating the iPad on screen results in the correct navigationBar graphic regardless of orientation.
On the device - it doesn't work - The graphic appears correctly on iPad device launch in portrait. When I rotate to landscape, the navigationBar changes to a standard grey one. When I rotate back the grey 'sticks'.
I have tried calling setNeedsDisplay
- no change.
I have tried setting navigationBar.tintColor = [UIColor clearColor]
- no change.
I have checked the filenames of the graphics in the code are identical to the actual filenames - they are.
Code for rotation:
-(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];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查
titleImage
是否为零。我感觉您的图像路径不正确,或者这些图像未正确复制到 iPad。Check whether
titleImage
is nil. I have a feeling that your image paths are not correct, or those images are not correctly copied to iPad.