Monday, May 7, 2012

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

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;

...

interface

...

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

type
{ TUIViewController }

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

{ TUIWindow }

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

// 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

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

implementation

...

function TUIViewController.shouldAutorotateToInterfaceOrientation(
AinterfaceOrientation: UIInterfaceOrientation): Boolean;
begin
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;
end;

...
Enjoy!

23 comments:

  1. @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.

    ReplyDelete
  2. 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... ;)

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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

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

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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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

    ReplyDelete
  9. Excellent! Thanks for letting me know. :)

    ReplyDelete
  10. Hi Anders! Would this code work under iOS6?

    ReplyDelete
  11. The North Face soon became a household name for outdoor gear. While many speculated the buzz to die down, the brand remains popular to this day, and for good reasons. Because the plain looks of the brand barely qualify as fashion, they are unlikely to go out of style. After all, nothing beats out vanilla. Furthermore, because the items are well-made, people have confidence that The North Face will perform well regardless of what the current trend is. Simplicity and high quality are again the magic behind this popular icon.

    ReplyDelete
  12. Burberry was founded in 1856 by the 21-year-old Thomas Burberry.an burberry sito ufficiale d then started its successful jo borse chanel sito ufficiale urney during weight problems,burberry sito ufficiale.they used to be the officer supplier and royal supplier.Empty the actual contents of the purse to launder it. Operate a borse chanel 2.55 lint brush or vacuum hose nex

    ReplyDelete
  13. How can I combine my love of the arts, especially creative writing with a law degree?

    ReplyDelete
  14. A friend has a computer that turns itself off after a certain period of time of inactivity. And all you have to do is move the mouse a little, and the computer comes back on. Maybe the computer is not completely off, maybe this is called hibernate or standby or something. Is this a good thing to do or should I just let my computer run? How do you get the computer to do this auto shut off thing as I described at the start of this paragraph?.

    ReplyDelete
  15. The Hacker’s Corner » Blog Archive » Modifying FMX_Platform_iOS.pas to support surface mainWindow and add device orientation support
    acfyoztqdyp
    [url=http://www.gg541y6k7g51epg314954vypta22zou9s.org/]ucfyoztqdyp[/url]
    cfyoztqdyp http://www.gg541y6k7g51epg314954vypta22zou9s.org/

    ReplyDelete
  16. I've seen other bloggers have more than one image per post, but the templates blogspot only allows one image per post. Does anyone know the .html or is there something else I have to do to get multiple images?.

    ReplyDelete
  17. The Hacker’s Corner » Blog Archive » Modifying FMX_Platform_iOS.pas to support surface mainWindow and add device orientation support
    kniyriqp http://www.gau4r73064pjxy0cs7f6j6k4487m7v5ps.org/
    [url=http://www.gau4r73064pjxy0cs7f6j6k4487m7v5ps.org/]ukniyriqp[/url]
    akniyriqp

    ReplyDelete
  18. Does your site have a contact page? I'm having problems locating it but, I'd like to shoot you an e-mail. I've got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it expand over time.|

    ReplyDelete
  19. For the reason that the admin of this site is working, no doubt very shortly it will be well-known, due to its feature contents.|

    ReplyDelete
  20. Supercharge your marketing campaign with our social media promotion services. Buy high quality likes/followers/views at highly competitive prices and with fast delivery.

    Get 5,000 Twitter Followers from:- http://khalaghor.com/twitter.html

    You can also order smaller or bigger package from our official website.

    ReplyDelete
  21. I don't even know how I ended up here, but I thought this post was great. I don't know who you are but definitely you are going to a famous blogger if you are not already ;) Cheers!|

    ReplyDelete
  22. I'm impressed, I must say. Seldom do I come across a blog that's both educative and engaging, and without a doubt, you've hit the nail on the head. The problem is something too few people are speaking intelligently about. I'm very happy that I came across this during my search for something concerning this.|

    ReplyDelete

Note: Only a member of this blog may post a comment.