我必须将单个 View Controller 锁定为横向,而其他 View Controller 则向所有方向旋转。我已经尝试过下面的代码。但 View 方向并没有像横向那样改变。请提出您的建议。
AppDelegate.cs
public bool RestrictRotation
{
get;
set;
}
[Export("application:supportedInterfaceOrientationsForWindow:")]
public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr
forWindow)
{
if (this.RestrictRotation)
return UIInterfaceOrientationMask.Landscape;
else
return UIInterfaceOrientationMask.All;
}
Viewcontroller.cs
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.Landscape;
}
public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
{
return UIInterfaceOrientation.LandscapeLeft;
}
void RestrictRotation(bool restriction)
{
AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate;
app.RestrictRotation = restriction;
}
最佳答案
您可以在ViewController中添加以下代码
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.allowRotation = true;
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.allowRotation = false;
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Unknown), new NSString("orientation"));
}
另外,不要忘记勾选 info.plist 中的 requires full screen ,否则在某些全屏设备(如 iPad Pro)上无法使用)。
关于ios - 方向锁定在 xamarin ios 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59151144/