2011-11-04 94 views
3

我創建了一個視圖控制器,並在XCode 4.2(界面生成器)中將視圖的方向設置爲橫向。但是,添加視圖時,視圖以縱向顯示。我已經覆蓋所有視圖控制器中的ShouldAutorotateToInterfaceOrientation以返回true,並且我試圖使用以下代碼手動旋轉視圖:單擊 - 在橫向上創建的視圖以縱向顯示

this.View.Transform.Rotate(3.14f * .5f);

我也嘗試將幀設置爲橫向幀(即480 X 320),儘管視圖的幀已經正確設置。

只是爲了澄清,我的絕大多數意見是在肖像。我希望以橫向方向加載橫向視圖,而不管設備實際位於何種方向。這是可能的嗎?如果是這樣,我錯過了什麼?

在此事先感謝您的幫助!

更新:我注意到ShouldAutorotateToInterfaceOrientation僅在視圖加載時調用一次。設備旋轉時不會調用它。這是正常的行爲嗎?

+0

當你說你重寫了ShouldAutorotateToInterfaceOrientation返回true ...你的意思是你正在返回以下?:return toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight; – Anuj

回答

-1

我認爲對於總是希望在橫向方向中顯示的橫向視圖,您需要在ShouldAutorotateToInterfaceOrientation內返回False或完全移除覆蓋。這將防止設備處於縱向模式時自動旋轉視圖。

+0

我已經嘗試了ShouldAutorotateToInterfaceOrientation的每個不同選項(返回true,返回false,全部刪除),並且行爲沒有改變。這個問題並不妨礙自動旋轉。該視圖根本不旋轉。它只是在肖像中顯示。XCode 4.2(界面生成器)中的視圖的「模擬度量標準」下的「方向」選項是否指定視圖應顯示在哪個方向,還是我需要做其他操作才能使方向旋轉? – Auc

+0

模擬指標中的設置都不會在運行時影響用戶界面,它們只會顯示「假設」。這是一個想法:你有沒有在你的Mono項目設置中指出你支持所有的設備方向?打開您的項目設置,展開Build,然後進入iPhone應用程序區域。在那裏,你會發現你的應用程序支持的方向列表。確保它們全部被選中,然後恢復ShouldAutorotateToInterfaceOrientation覆蓋並返回true。 –

+0

感謝competent_tech,但仍然沒有變化。我認爲指定一個方向將是微不足道的,但顯然,我錯了。我是否也應該通過this.View.Transform.Rotate旋轉視圖(3.14f * 0.5f)?儘管這條線似乎不影響任何方式。 – Auc

4

每次設備方向更改時都會調用ShouldAutorotateToInterfaceOrientation。

public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) 
     { 
      Console.Out.WriteLine(toInterfaceOrientation.ToString()); 
      // Return true for supported orientations 

      // This particular screen is landscape only! 
      return (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight); 
     } 

此代碼將只允許以使自身在任一landscapeleft或landscaperight模式,以及新設備的方向每次寫入控制檯。

時加載的觀點,並將其推入(例如)一個UINavigationController然而,它仍處於縱向模式(而ShouldAutorotateToInterfaceOrientation不叫。)

從Objective-C的

,這也很容易給力設備方向,但在MonoTouch中,似乎沒有直接映射。

該解決方案;

發送消息到objective-c運行時,指定我們想要的方向。

可以很容易地通過加入以下視圖控制器執行以下操作:

Selector orientationSetter; 

     private void SetOrientation (UIInterfaceOrientation toOrientation) 
     { 
      if (orientationSetter == null) 
      { 
       orientationSetter = new Selector ("setOrientation:"); 
      } 

      Messaging.void_objc_msgSend_int (UIDevice.CurrentDevice.Handle, 
      orientationSetter.Handle, (int)toOrientation); 
     } 

現在可以手動改變整個裝置的取向。更重要的是,如果你使用UINavigationController,一旦這個視圖彈出堆棧,方向將恢復正常。

相關問題