2012-03-02 70 views
1

我正在構建一個monoTouch-iPad應用程序,並且由於啓動界面方向而陷入困境。以正確的方向以編程方式顯示UIView的最佳做法

一個問題是,當應用程序啓動時UIDevice.CurrentDevice.Orientation總是返回Unknown。你如何決定你的應用程序在哪個方向啓動?我現在發現的所有屬性都只返回肖像模式,未知模式或肖像模式的幀大小 - 即使它是橫向模式。

我還創建了兩個UIViews(一個用於橫向,一個用於縱向),現在在UIViewController的WillRotate方法中更改它們。但我的代碼:

if(toInterfaceOrientation==UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation==UIInterfaceOrientation.LandscapeRight){ 

     _scrollView.RemoveFromSuperview(); 
     this.View.Add (_scrollViewLandscape); 
     }else{ 
     _scrollViewLandscape.RemoveFromSuperview(); 
     this.View.Add (_scrollView); 
} 

旋轉屏幕時會產生短而難看的「閃爍」 - 至少在模擬器中。

是否有佈置視圖的最佳做法?我知道ShouldAutorotateToInterfaceOrientation,但這對我不起作用,因爲我正在做很多所有者繪製的東西,這些東西在自動修復後會被破壞(see my other question)。

我真的很感謝沒有使用Interface-Builder的解決方案,因爲我現在正在做所有的代碼。

UPDATE:短描寫的特徵我想達到的目標: AppStart的 - >知道正確Framsize(1024,748或768,1004) - >添加我的自定義在正確的框架尺寸

UPDATE2觀點:簡單和基本的代碼片段

public override void ViewDidLoad() 
    { 
     base.ViewDidLoad();    
     Console.WriteLine (this.InterfaceOrientation); 
    } 

返回肖像。即使模擬器處於橫向模式。

回答

2

內UIViewController中可以只檢查InterfaceOrientation

public override void ViewDidLoad() 
{ 
    if (this.InterfaceOrientation == UIInterfaceOrientation.Portrait 
     || this.InterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown) 
    { 
     // portrait 
    } 
    else 
    { 
     // landsacpe 
    } 
} 

,但我真的建議使用View.AutoresizingMask或壓倒一切的LayoutSubviews,既讓所有的轉換真的順利

更新:使用AutoresizingMask

public override void ViewDidLoad() 
{ 
    UIView view = new CustomView(View.Bounds); 
    view.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth; 
    View.AddSubview(view); 
} 

U PDATE:覆蓋LayoutSubviews

LayoutSubviews被稱爲每次大小的變化

public class CustomView : UIView 
{ 
    public override void LayoutSubviews() 
    { 
     //layout your view with your own logic using the new values of Bounds.Width and Bounds.Height 
    } 
} 
+0

很抱歉,但在應用程序啓動'this.InterfaceOrientation'返回'Portrait'即使模擬器是在橫向模式。 – basti 2012-03-02 11:00:35

+0

如何正確使用擁有所有視圖的AutoresizingMask?重寫LayoutSubviews有什麼好處? – basti 2012-03-02 11:03:10

+0

我無法使用Autoresizing Masks工作,但我應該如何在LayoutSubviews中工作?藉助轉換或更換整個視圖? – basti 2012-03-02 12:04:12