无法在Portrait,xamarin IOS中锁定一个视图控制器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在Portrait,xamarin IOS中锁定一个视图控制器相关的知识,希望对你有一定的参考价值。
我试图只允许一个视图控制器锁定在纵向,同时允许所有其他视图是任何方向。这就是我试图放入我的homeViewController(我想保留的肖像)。
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
{
return UIInterfaceOrientationMask.Portrait;
}
public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
{
return UIInterfaceOrientation.Portrait;
}
public override bool ShouldAutorotate()
{
return false;
}
我试图在c#中的xamarin中这样做。有人有什么建议吗?
答案
在AppDelegate.cs中添加它
public bool RestrictRotation
{
get;
set;
}
并在FinishedLaunching()方法之后在相同的AppDelegate.cs中添加它
[Export("application:supportedInterfaceOrientationsForWindow:")]
public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr
forWindow)
{
if (this.RestrictRotation)
return UIInterfaceOrientationMask.Portrait;
else
return UIInterfaceOrientationMask.All;
}
将以下内容添加到您要创建Portrait的每个viewcontroller中
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;
}
另一答案
我从一个基地派生我的控制器。我需要这个用于其他目的,但也用于锁定纵向方向
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();
}
}
以上是关于无法在Portrait,xamarin IOS中锁定一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin:无法在 iOS 9.2.1 设备上运行应用程序
Xamarin.IOS FFimageloading无法加载