2016-11-10 9 views
1

1つのView Controllerのみをポートレートでロックし、他のすべてのビューは任意の向きにするようにしています。これは私が私のhomeViewController(私は肖像画で保持したいもの)に入れようとしているものです。ポートレートのxamarinで1つのView Controllerをロックできません。

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() 
    { 
     return UIInterfaceOrientationMask.Portrait; 
    } 
    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation() 
    { 
     return UIInterfaceOrientation.Portrait; 
    } 
    public override bool ShouldAutorotate() 
    { 
     return false; 
    } 

私はC#のxamarinでこれを実行しようとしています。誰か提案がありますか?

+0

あなたのコードは、 'UIViewController'の' PresentViewController'を別の 'UIViewController'としていますが、' UINavigationController'を使用していますか?そして/またはあなたの 'homeViewController'は、アプリの起動(?)に表示される最初のVCで、ポートレートでなければならず、その後は向きがロックされているすべてのVCが必要ですか? – SushiHangover

+0

はい私はUINavigationControllerを使用しています。 homeViewControllerが最初に読み込まれたものです。私はちょうどその肖像画でロックされたビューと残りの任意の方向にしたい。 –

答えて

0

私は1つのベースからコントローラを派生させます。私は

public class BaseView : UIViewController 
{ 

    static List<Type> SupportingLandscapeScreenTypes = new List<Type>() 
    { 
     typeof(TemperaturesHistoryView), 
     typeof(LoadSwitchConsumptionView), 
     typeof(HomeConsumptionGraphView) 

    }; 

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() 
    { 
     foreach (var screenType in SupportingLandscapeScreenTypes) 
     { 
      if (GetType() == screenType) 
       return UIInterfaceOrientationMask.Portrait | UIInterfaceOrientationMask.LandscapeLeft | UIInterfaceOrientationMask.LandscapeRight; 
     } 
     return UIInterfaceOrientationMask.Portrait; 
    } 

    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation() 
    { 
     return UIInterfaceOrientation.Portrait; 
    } 

} 

public class MyEnergateAppNavigationController:UINavigationController 
{ 
    public MyEnergateAppNavigationController(UIViewController rootController) 
     :base (rootController) 
    { 
    } 

    public override bool ShouldAutorotate() 
    { 
     return true; 
    } 

    //[Obsolete ("Deprecated in iOS6. Replace it with both GetSupportedInterfaceOrientations and PreferredInterfaceOrientationForPresentation")] 
    //public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation) 
    //{ 
    // return TopViewController.ShouldAutorotateToInterfaceOrientation(toInterfaceOrientation); 
    //} 

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() 
    { 
     return TopViewController.GetSupportedInterfaceOrientations(); 
    } 

    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation() 
    { 
     return TopViewController.PreferredInterfaceOrientationForPresentation(); 
    } 
} 
+0

もしあなたが望むなら、GetSupportedInterfaceOrientationsでLINQを使うこともできます –

+0

これは全く違うクラスですか?アプリの起動時にhomeViewControllerを持っていて、それがナビゲーションコントローラです。このコードをそのクラスに入れるか、別のクラスを作成する必要がありますか? –

+0

ナビゲーションコントローラを含むように更新されました。すべての「内部」コントローラは、答えに示されているようにベースコントローラから派生しています。 SupportingLandscapeScreenTypesを静的に定義して、複数のインスタンスを避けることができます –

0

public bool RestrictRotation 
    { 
     get; 
     set; 
    } 

AppDelegate.cs

にこれを追加し、同じAppDelegate.csでこれを加えるが、FinishedLaunching()メソッドの後に、他の目的のためにこれを必要とするだけでなく、縦方向にロックするために使用する

[Export("application:supportedInterfaceOrientationsForWindow:")] 
    public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr 
     forWindow) 
    { 
     if (this.RestrictRotation) 
      return UIInterfaceOrientationMask.Portrait; 
     else 
      return UIInterfaceOrientationMask.All; 
    } 

作成したいすべてのビューコントローラに以下を追加します。ポートレート

public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     this.RestrictRotation(true); 
     // Perform any additional setup after loading the view, typically from a nib. 
    } 

    public override bool ShouldAutorotate() 
    { 
     return false; 
    } 

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() 
    { 
     return UIInterfaceOrientationMask.Portrait; 
    } 

    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation() 
    { 
     return UIInterfaceOrientation.Portrait; 
    } 

    void RestrictRotation(bool restriction) 
    { 
     AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate; 
     app.RestrictRotation = restriction; 
    } 
関連する問題