2017-03-28 82 views
1

我在Xamarin mvvmcross中提供了支持IOS縱向和橫向屏幕方向的解決方案。 IOS 8.0 我想添加新的MvxViewController,但只在肖像模式下鎖定它。 我在互聯網範例中搜索了ios swift,xamarin表單,但所有這些都不適用於mvvmcross解決方案 有沒有辦法讓我可以鎖定肖像模式只用於Xamarin mvvmcroos的一個屏幕?mvvmcross ios鎖定特定MvxViewController的縱向方向

謝謝

回答

2

這和普通的ViewController沒有區別。

public override bool ShouldAutorotate() => false; 
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() => 
    UIInterfaceOrientationMask.Portrait; 
+0

感謝您的回答,我說代碼到我的控制器,但ShouldAutorotate仍然沒有被調用。 public override bool FinishedLaunching(UIApplication app,NSDictionary options) window = new UIWindow(UIScreen.MainScreen。邊界); var presenter = new MvxIosViewPresenter(this,window); var setup = new Setup(this,presenter); setup.Initialize(); var startup = Mvx.Resolve (); startup.Start(); window.MakeKeyAndVisible(); 返回true; } – Andrei

+0

也許我需要改變我的mvvmcross AppDelegate文件中的內容?謝謝 – Andrei

+0

我認爲蘋果已經改變了iOS 8以後輪換的方式。我知道它在iOS 10中絕對不同。 – Digitalsa1nt

0

其實我正在這種方法: 在應用程序的AppDelegate:MvxApplicationDelegate類我創建

public bool RestrictRotation = true; 

,並添加方法:

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

然後視圖,我只想肖像模式,我用

public override void ViewWillAppear(bool animated) 
{ 
    base.ViewWillAppear(animated); 

    AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate; 
    app.RestrictRotation = false; 
} 

,並支持所有的屏幕旋轉我設置:

public override void ViewWillAppear(bool animated) 
{ 
    base.ViewWillAppear(animated); 

    AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate; 
    app.RestrictRotation = true; 
} 

希望這將有助於! 謝謝

0

我需要一個應用程序,是所有畫像,除了一定的模態意見的風景,所以我做了這樣的iOS 10 MvvmCross 5.0:

首先設置你的應用程序將需要在取向Info.plist的。

然後,您將需要找到您的[MvxRootPresentation],即您的根視圖。

根的觀點是,將接收來自ShouldAutorotate & GetSupportedInterfaceOrientations呼叫然後我通過這些下降到可見控制器,如果他們同時實現這些方法是虛擬方法,從而如果沒有實現的默認是ShouldRotate =假GetSupportedInterfaceOrientations所述一個= AllButUpsideDown

在根視圖添加ShouldAutorotate & GetSupportedInterfaceOrientations方法是這樣的:

public override bool ShouldAutorotate() 
{ 
    return true; // Allows all view to auto rotate if they are wrong orientation 
} 

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() 
{ 
    var orientation = UIInterfaceOrientationMask.Portrait; 

    if (VisibleUIViewController != null) 
    { 
     var VisibleMvxViewController = VisibleUIViewController as MvxViewController; 
     if (VisibleMvxViewController != null) 
     { 
      // Check if the view is a Modal View by checking for the MvxModalPresentationAttribute 
      var attribute = VisibleMvxViewController.GetType().GetCustomAttributes(typeof(MvxModalPresentationAttribute), true).FirstOrDefault() as MvxModalPresentationAttribute; 
      if (attribute != null) 
      { 
       // Visible view is a modal 
       if (!VisibleUIViewController.IsBeingDismissed) 
       { 
        // view is not being dismissed so use the orientation of the modal 
        orientation = VisibleUIViewController.GetSupportedInterfaceOrientations(); 
       } 
      } 
     } 
    } 

    return orientation; 
} 

然後在模態次(即HAV E中的MvxModalPresentation在我的情況屬性)要改變方向重寫此方法:

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

這種解決方案意味着你不需要任何東西添加到AppDelegate中