我想用平板电脑友好的用户界面扩展一个应用程序。由于手机用户界面在纵向方向上的表现要好得多,所以现有的应用程序被锁定在纵向;而平板电脑版本则没有这一限制。
这在Android上运行良好;主要活动决定在启动时是允许所有设备方向(在平板电脑上)还是将应用程序锁定到肖像(在手机上)。问题还在于在iOS上实现定向逻辑。
我知道Xamarin.Forms有一个bug,它可以防止iOS设备上的代码控制设备方向。我们提出了一个可能的解决方法:所有构建目标都是重复的(一个实例用于手机,另一个实例用于平板电脑),手机和平板电脑的构建分别上载到App Store。
这真的是实现特定于习惯用法的设备定向的最优雅的方法吗?还是有一种方法可以在不太麻烦的情况下绕过Xamarin.Forms bug?
发布于 2016-11-11 22:03:53
下面是它的魔力:
AppDelegate.cs
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
{
//Tablet orientation adjustment
if (Device.Idiom == TargetIdiom.Tablet)
return UIInterfaceOrientationMask.Landscape;
else
return UIInterfaceOrientationMask.Portrait;
}然后为ContentPage创建一个CustomRenderer并覆盖OnElementChangedMethod
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
Page _page = Element as Page;
_page.Appearing += (s, ea) =>
{
UIInterfaceOrientation orientain = Device.Idiom == TargetIdiom.Phone ? UIInterfaceOrientation.Portrait : UIInterfaceOrientation.LandscapeLeft;
NSNumber number = NSNumber.FromInt32((int)orientain);
UIDevice.CurrentDevice.SetValueForKey(number, new NSString("orientation"));
UIViewController.AttemptRotationToDeviceOrientation();
};
}
}https://stackoverflow.com/questions/40547092
复制相似问题