基本上我想要实现的是当用户在应用程序的某个部分根据需要更改屏幕旋转时,我有这个为Andriod工作,我不明白为什么它不应该适用于iOS
procedure TForm1.Button1Click(Sender: TObject);
var
ScreenService: IFMXScreenService;
OrientSet: TScreenOrientations;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService))
then
begin
OrientSet := [TScreenOrientation.soLandscape];//<- Break point set here and line is executed
ScreenService.SetScreenOrientation(OrientSet);
end;
end;
从这里采取:How to prevent screen rotation with android development in delphi xe5 Firemonkey
ScreenService.SetScreenOrientation已执行且不会引发异常,但方向未更改,我还在项目&gt;选项&gt;应用程序&gt;方向中设置启用自定义方向,但这也没有&#39 ; t有任何影响。
对我来说,奇怪的是,如果它不受支持,则不应该
if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService))
返回false?甚至没有进入开始
我添加了一个测试按钮来检查屏幕方向,之后我只用
将其设置为横向if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService))
then
begin
case ScreenService.GetScreenOrientation of
TScreenOrientation.Portrait: ShowMessage('Portrait');
TScreenOrientation.Landscape: ShowMessage('landscape');
TScreenOrientation.InvertedPortrait: ShowMessage('Inverted-Portrait');
TScreenOrientation.InvertedLandscape: ShowMessage('Inverted-Landscape');
else ShowMessage('not set');
end;
end;
如果在将它设置为横向之后它在纵向中仍然显示纵向
更新1 :我也尝试过更改
OrientSet := [TScreenOrientation.soLandscape] // <- Deprecated
到
OrientSet := [TScreenOrientation.Landscape]
但行为仍然相同
答案 0 :(得分:5)
好的这次轮换让我深入了解iOS API,以了解iOS如何管理方向。
因为您无法在iOS中的iOS中伪造旋转或强制设备某些设备方向,因此您需要考虑很多方向
无法强制或更改设备方向,这将始终显示设备现在的方向。
然而,状态栏方向始终具有您可以调用的setStatusBarOrientation
过程并且可以指定您想要的方向,但是这被标记为已弃用,因此无效,但我没有获得外部我打电话给这个函数的例外情况,它仍然存在,它只是没有工作。
然后我读到了这个:
setStatusBarOrientation:animated:方法不会被彻底弃用。它现在只有在最顶层的全屏视图控制器的supportedInterfaceOrientations方法返回0时才有效
然后我决定尝试这个
Application.FormFactor.Orientations := []; //the supportedInterfaceOrientations returns 0
App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
win := TUIWindow.Wrap(App.windows.objectAtIndex(0));
App.setStatusBarOrientation(UIInterfaceOrientationLandscapeLeft);
它可以处理状态栏方向更改,但是如上所述,此协议已弃用,因此在某个阶段它可能/将会消失,这将不再有效。
但这只改变了状态栏和所有警报视图的旋转等,但是在我想要更改为横向的情况下,rootviewcontroller中的实际内容仍然在Portrait中。
然后,如果我愿意在Delphi中转到Application-&gt; Orientation-&gt;启用自定义旋转并说景观然后应用程序将仅以横向显示,并且它完美地完成,因为在创建表单然后创建rootViewcontroller时,返回的supportedInterface方向仅为横向并且Viewcontroller将转到横向。
因为当您的设备更改设备方向时,将根据Viewcontroller支持的接口方向评估方向,如果不支持方向,则不会旋转方向。
但是当分配新的rootviewcontroller时,必须更新GUI以在Viewcontrollers支持的方向中显示,考虑到这一点,这是我的修复/黑客将应用程序的方向更改为您想要的方向。
<强> TL; DR 强>
Uses: iOSapi.UIKit;
procedure ChangeiOSorientation(toOrientation: UIInterfaceOrientation;
possibleOrientations: TScreenOrientations);
var
win : UIWindow;
App : UIApplication;
viewController : UIViewController;
oucon: UIViewController;
begin
Application.FormFactor.Orientations := []; //Change supported orientations
App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
win := TUIWindow.Wrap(App.windows.objectAtIndex(0)); //The first Windows is always the main Window
App.setStatusBarOrientation(toOrientation);
{After you have changed your statusbar orientation set the
Supported orientation/orientations to whatever you need}
Application.FormFactor.Orientations := possibleOrientations;
viewController := TUIViewController.Wrap(TUIViewController.alloc.init);//dummy ViewController
oucon := TUIViewController.Wrap(TUIViewController.alloc.init);
{Now we are creating a new Viewcontroller now when it is created
it will have to check what is the supported orientations}
oucon := win.rootViewController;//we store all our current content to the new ViewController
Win.setRootViewController(viewController);
Win.makeKeyAndVisible;// We display the Dummy viewcontroller
win.setRootViewController(oucon);
win.makeKeyAndVisible;
{And now we Display our original Content in a new Viewcontroller
with our new Supported orientations}
end;
现在你所要做的就是致电ChangeiOSorientation(toOrientation,possibleOrientations)
如果您希望它转到纵向,但可以选择横向作为
ChangeiOSorientation(UIInterfaceOrientationPortrait,[TScreenOrientation.Portrait,TScreenOrientation.Landscape,TScreenOrientation.InvertedLandscape]);
这正在进行