Modifying FMX_Platform_iOS.pas to support surface mainWindow and add device orientation support

Posted by on in Blogs
Here are the steps that I use in order to surface a couple of things that some of my custom iOS components and apps need.

These modifications are not supported by anyone. Except maybe me. You apply them entirely at your own risk.

Modify FMX_Platform_iOS.pas on your MAC as below. It's typically located in the /Developer/Embarcadero/fmi directory.

CAUTION: Save a copy of FMX_Platform_iOS.pas FIRST!
CAUTION: Any update of XE2 will delete your changes, so keep this file around if you have to redo the changes.


unit FMX_Platform_iOS;




// TUIViewController and TUIWindow declarations moved from implementation to interface section

{ TUIViewController }

TUIViewController = objcclass(UIViewController)
function shouldAutorotateToInterfaceOrientation(AinterfaceOrientation: UIInterfaceOrientation): Boolean; override;
procedure didReceiveMemoryWarning; override;
procedure didAnimateFirstHalfOfRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation); override;
procedure didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation); override;

{ TUIWindow }

TUIWindow = objcclass(UIWindow)
Text: UITextField;
mainView: UIView;
mainController: TUIViewController;

// mainWindow moved from implementation to interface section
// currentOrientation added to give current device orientation
// supportsPortrait etc added. You simply change these in your app code to False if not needed

mainWindow: TUIWindow;
currentOrientation : UIInterfaceOrientation;
supportsPortrait : Boolean = True;
supportsPortraitUpsideDown : Boolean = True;
supportsLandscapeRight : Boolean = True;
supportsLandscapeLeft : Boolean = True;



function TUIViewController.shouldAutorotateToInterfaceOrientation(
AinterfaceOrientation: UIInterfaceOrientation): Boolean;
Result := ((AinterfaceOrientation = UIInterfaceOrientationLandscapeRight) and supportsLandscapeRight) or
((AinterfaceOrientation = UIInterfaceOrientationLandscapeLeft) and supportsLandscapeLeft) or
((AinterfaceOrientation = UIInterfaceOrientationPortrait) and supportsPortrait) or
((AinterfaceOrientation = UIInterfaceOrientationPortraitUpsideDown) and supportsPortraitUpsideDown);

if Result then
currentOrientation := AinterfaceOrientation;



  • Guest
    Olecramoak Monday, 7 May 2012

    @Anders: what does it do exactly? Could not figure out..

    is it to avoid device rotation? Intersting that compiled XE2 app ignores rotation preferences from Xcode. Does this override that?

    I read somewhere that Apple is not a fan of non-rotateble apps and it might get rejected for it. But what do we know, I guess each case is avaliated individually by them.

  • Guest
    Anders Ohlsson Monday, 7 May 2012

    DOH! I can't believe I forgot to include the implementation part as well.

    shouldAutorotateToInterfaceOrientation has to be changed of course.

    Blog edited to reflect all changes... ;)

  • Guest
    Anders Ohlsson Monday, 7 May 2012

    And yes, the default behaviour is the same - all rotations are supported. If you set a few to False, then the UI won't rotate to those.

    You don't have to support all 4 rotations at all. Typically you support at least 2. I can't see why you wouldn't support Portrait AND PortraitUpsideDown if you support one of them, and likewise, I can't see why you wouldn't support LandscapeRight AND LandscapeLeft if you support one of them.

    My first app - my analog clock - only supports Portrait. If I ever update it again, I'll probably support all UI rotations with proper scaling. The MathViz update will only support Landscap (Left/Right) going forward, simply because the controls constrict the middle of the screen too much. At least the way I designed it.

  • Guest
    Alan Thursday, 21 June 2012

    Hi Anders
    I am confused as to the modification of the FMX_Platform_iOS.pas file, do I simply add what you have shown above to the FMX_Platform_iOS.pas file or do I also have to remove the similar declarations that exist in that file as well?
    Thank you

  • Guest
    Anders Ohlsson Thursday, 21 June 2012

    @Alan: The comments tell you what to do. It's mainly a matter of moving things from implementation to interface.

  • Guest
    Alan Thursday, 21 June 2012

    No probs, just wanted to be sure I didn't duplicate anything. Thanks

  • Guest
    Alan Sunday, 15 July 2012

    H Anders
    Can I be a pain and ask that you email me the modified FMX_Platform_iOS.pas as I just keep getting 'script invocation error' when I do it myself? Or post the modified FMX_Platform_iOS.pas here whichever suits. I would really be grateful.
    Thank you

  • Guest
    Alan Monday, 16 July 2012

    All ok now! Doh!
    I kept placing it under 'Type' but not including it as part of itself. Anyway, silly mistake that's all sorted now. Thanks

  • Guest
    Anders Ohlsson Tuesday, 17 July 2012

    Excellent! Thanks for letting me know. :)

  • Guest
    Olecramoak Wednesday, 26 September 2012

    Hi Anders! Would this code work under iOS6?

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: