2011-11-04 60 views
0

我最近更新了我的iPad到iOS5,發現了一個相當奇怪的問題。我的應用程序中有兩種不同的佈局。我通過參考self.interfaceOrientation更新didRotateFromInterfaceOrientation方法中的視圖。但現在self.interfaceOrientation沒有得到更新,並且視圖顯示出奇怪。任何人都可以在這個問題上拋出一些光。有什麼新的iOS 5給我這個麻煩?「self.interfaceOrientation」未在iOS 5中更新

回答

0

我不知道iOS 5有什麼變化,但是這裏是我自iOS 4以來使用的代碼,並且從此開始工作得很好。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 
    switch (toInterfaceOrientation){ 

     case UIInterfaceOrientationPortrait: 
     case UIInterfaceOrientationPortraitUpsideDown: 

     //change layout to Portrait 

     return YES; 

     case UIInterfaceOrientationLandscapeLeft: 
     case UIInterfaceOrientationLandscapeRight: 

     //change layout to Landscape 

     return YES; 

     default: 
      return NO; 
    } 
} 
+1

這並沒有回答這個問題 –

+0

我提供了一種替代方法來實現他的佈局,當方向改變時。這種替代方式解決了問題中描述的錯誤。 – Louis

+0

啊,我明白了;您建議將佈局更改插入您的評論。我誤解了。儘管如此,這仍然是一個糟糕的主意。 ''''''''''''''''''''''''''''''''''''如果將控制器放在導航控制器中,它將快速連續調用該方法4次並緩存返回值。 –

0

您可以使用

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

,並設置定向變量,但它不是一個很好的解決方案。

0

我認爲這裏的問題是你實際上抓住設備方向的方式。以我的經驗self.interfaceOrientation並不總是可靠的。

有幾種方法來獲取當前UIInterfaceOrientation,但是從我的理解,從狀態欄獲得的方位是唯一比較有效的方法使用代替嘗試:

[UIApplication sharedApplication].statusBarOrientation

提醒 - UIInterfaceOrientationUIDeviceOrientation是不一樣的!它們也可能會在不同的時間更新,具體取決於您如何初始化它們。

你也可以在類似的問題上檢查this answer