方向锁在xamarin ios中不起作用
我必须将单个viewcontroller锁定为横向,其他viewcontroller全部旋转。我已经尝试了下面的代码。但视图方向不随景观而改变。请给出你的建议 AppDelegate.cs方向锁在xamarin ios中不起作用,ios,xamarin.ios,orientation,Ios,Xamarin.ios,Orientation,我必须将单个viewcontroller锁定为横向,其他viewcontroller全部旋转。我已经尝试了下面的代码。但视图方向不随景观而改变。请给出你的建议 AppDelegate.cs public bool RestrictRotation { get; set; } [Export("application:supportedInterfaceOrientationsForWindow:")] public UIInterfaceOrientationMask GetSup
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中的要求全屏,否则它在某些全屏设备(如iPad Pro)上无法工作
appDelegare中没有“allowRotation”属性。请使用
RestrictRotation
。不,viewcontroller仍然没有旋转。它在我这边工作正常,您可以共享您的示例,我将在我这边测试。请从链接中查找示例附件。