我试图让一个视图控制器锁定在纵向,同时允许所有其他视图为任何方向。这就是我试图放入我的homeViewController(我想保留的肖像)。
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
{
return UIInterfaceOrientationMask.Portrait;
}
public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
{
return UIInterfaceOrientation.Portrait;
}
public override bool ShouldAutorotate()
{
return false;
}
我试图在c#中的xamarin中这样做。有人有什么建议吗?
答案 0 :(得分:2)
在AppDelegate.cs中添加此内容
public bool RestrictRotation
{
get;
set;
}
然后在同样的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;
}
答案 1 :(得分: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();
}
}