Delphi/Firemonkey 在运行时更改 iOS 屏幕旋转

Delphi/Firemonkey Change iOS screen rotation at runtime

基本上我想要实现的是当用户在应用程序的某个部分根据需要更改屏幕旋转时,我已经为 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 被执行并且没有引发异常但是方向没有改变,我还在项目>选项>应用>中设置了启用自定义方向方向但这也没有任何影响。

令我感到奇怪的是,如果它不受支持,那么它不应该

if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) 

Return假的?甚至不输入 begin

我添加了一个测试按钮来检查屏幕方向后我将它设置为仅横向

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]

但行为仍然相同

好的,这次轮换让我深入研究了 iOS API 以弄清楚 iOS 如何管理方向。

因为你不能假装旋转或强制设备确定设备方向 in iOS in iOS 你有很多方向需要考虑

  • 设备方向
  • 状态栏方向
  • UIViewcontroller方向

无法强制或更改设备方向,这将始终显示您的设备当前的方向。

然而,状态栏方向一直有 setStatusBarOrientation 过程,您可以调用并指定您想要的方向,但这被标记为已弃用,因此无法正常工作,但我没有得到外部当我调用该函数时出现异常,表明它仍然存在,只是无法正常工作。

然后我读了这个:

The setStatusBarOrientation:animated: method is not deprecated outright. It now works only if the supportedInterfaceOrientations method of the top-most full-screen view controller returns 0

然后我决定试试这个

Application.FormFactor.Orientations := []; //the supportedInterfaceOrientations returns 0
App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
win := TUIWindow.Wrap(App.windows.objectAtIndex(0));
App.setStatusBarOrientation(UIInterfaceOrientationLandscapeLeft);

它可以改变状态栏方向,但如上所述,这个协议已被弃用,所以在某个阶段它 can/will 会消失,这将不再有效。

但这只是改变了状态栏和所有警报视图等的旋转,但在我想要更改为横向的情况下,rootviewcontroller 中的实际内容仍在纵向中。

然后我想如果我在 Delphi 会去 Application->Orientation->Enable custom rotation 并说 landscape only 然后 App 将只显示在 landscape 中,它做得很好,因为当表单被创建,然后 rootViewcontroller 被创建返回的 supportedInterface 方向只有横向,Viewcontroller 转到横向。

因为当您的设备更改设备方向时,将根据 Viewcontroller 支持的界面方向评估方向,如果不支持方向,则不会旋转。

但是当分配了一个新的 rootviewcontroller 时,必须更新 GUI 以显示 Viewcontrollers 支持的方向,考虑到这一点,这里是我的 fix/hack 来改变你的方向应用到你想要的方向。

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]);    

正在处理

  • Delphi 10 西雅图
  • iPhone 5 运行 iOS 9.0
  • PA 服务器 17.0
  • Xcode 7.1
  • iPhoneOS 9.1 SDK