Jump to content
gioma

[Firemonkey ]Change iOS screen rotation at runtime

Recommended Posts

Hi,

I need to rotate the screen at runtime when I open a form.
Until the release of IOS 13 this code worked perfectly:

 

procedure ChangeOrientation(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;

However, since the release of IOS 13 the screen locks on the set orientation but does not rotate. So if I set Landscape orientation the screen remains in Portrait orientation until I physically turn the phone around, at which point it rightly remains locked on that orientation.
This is a real disaster because then, until the phone is turned, all the controls ( finger position, object position,..) are wrong.

 

Share this post


Link to post

Looking for a solution too, since I used same code as yours.
Seems that SetStatusBar was deprecated, and now in iOS 13 its probably gone.
https://stackoverflow.com/questions/7030682/ios-iphone-ipad-sdk-alternative-for-uiapplication-sharedapplication-setst

https://developer.apple.com/documentation/uikit/uiapplication/1623026-statusbarorientation

 

 

Edited by Rollo62

Share this post


Link to post

Yes, I know that the SetStatusBar function is obsolete and now there is no other function to do it.
I found a solution, to open the module that I would like to show only in Landscape, I show a message to the user to inform him that he must rotate the phone to open it.
Then, when the user rotates the phone, the "onResize" event is activated. In this event the orientation is set only to Landscape and then the form is opened.

Share this post


Link to post

Ok, that is a workaround.
But I hope that there is a better way ... Apple strikes back on iOS13 again ... I'm just still fixing other similar issues.

Share this post


Link to post

I still have a "manual" solution implemented, which gives advice to the user howto set-up the right orientation.

Not perfect, but no time to search for a better one right now.

Since its happening only in a remore place in one app, its acceptable for me.

 

Share this post


Link to post
24 minutes ago, Rollo62 said:

I still have a "manual" solution implemented, which gives advice to the user howto set-up the right orientation.

In my case, it's not about advising the user how to set up anything: Application.FormFactor.Orientations takes care of which orientations the app will support. The issue is with re-orienting the app once Application.FormFactor.Orientations has been changed at runtime. Currently, the user has to rotate the device themselves.

Share this post


Link to post

Thats what I meant too:
- if device is currently in landscape,
- the user switches a button to show a  new page which requires Portrait mode
- give the user an advice that turning the device is necessary before, or
- possible too that the new mode appears automatically, when the user has turned the device into portrait

 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×