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