【发布时间】:2019-03-26 07:21:26
【问题描述】:
我使用依赖服务来强制 Android 和 iOS 中单个页面的横向, 这是安卓版的:
public class OrientationService : IOrientationService
{
public void Landscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void Portrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
它工作得很好并且根据需要:强制横向模式,即使手头的设备方向是纵向的,我也需要为 iOS 实现相同的功能,尝试了这个(也尝试了注释代码):
public class OrientationService : IOrientationService
{
public void Landscape()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
//((AppDelegate)UIApplication.SharedApplication.Delegate).CurrentOrientation = UIInterfaceOrientationMask.Landscape;
//UIApplication.SharedApplication.SetStatusBarOrientation(UIInterfaceOrientation.LandscapeLeft, false);
}
public void Portrait()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
//((AppDelegate)UIApplication.SharedApplication.Delegate).CurrentOrientation = UIInterfaceOrientationMask.Portrait;
//UIApplication.SharedApplication.SetStatusBarOrientation(UIInterfaceOrientation.Portrait, false);
}
}
但这只有在设备处于横向模式时才会切换到横向,不像 Android 版本
【问题讨论】:
标签: ios xamarin xamarin.ios screen-orientation