Delphi / Firemonkey Change iOS screen speed at runtime

Basically, all I want to achieve is when the user is in a certain part of the application to change the rotation of the screen as necessary, it works for me for Andriod, and I don’t understand why it should not work for 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;

Taken from here: How to prevent screen rotation while developing Android in delphi xe5 Firemonkey

ScreenService.SetScreenOrientation is executed and does not throw an exception, but the orientation does not change, I also set the Enable custom orientation to Project> Options> Application> Orientation, but it also didn’t help have any effect.

What is strange to me is that if it was not supported, then this should not be

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]

+4
1

, iOS API, , iOS .

iOS iOS, ,

  • UIViewcontroller

, , .

, setStatusBarOrientation, , , , , , , , , .

:

setStatusBarOrientation: : . , 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 , .

, Delphi Application- > Orientation- > Enable custom rotation , , , , , rootViewcontroller , , , Viewcontroller - .

, , Viewcontroller , , .

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

  • Delphi 10 Seattle
  • iPhone 5 iOS 9.0
  • PA 17.0
  • Xcode 7.1
  • iPhoneOS 9.1 SDK
+5

Source: https://habr.com/ru/post/1613457/


All Articles