[英]Orientation lock is not working in xamarin ios
我必須將單個視圖控制器鎖定為橫向,而其他視圖控制器則全部旋轉。 我試過下面的代碼。 但是視圖方向不會像風景那樣改變。 請提出您的建議。
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;
}
視圖控制器.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)上將無法運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.