delphi APP 横屏和竖屏


本文整理自网络,侵删。

 
普通情况下,APP 随着用户拿手机是横向还是竖向,自动翻转 -- 当然,这个也需要用户设置手机的屏幕是否跟随手机的物理方向而翻转。

但如果我们的APP,在某种情况下,必须以竖屏方式显示,在某种情况下,必须以横屏方式显示,不管用户是否把手机横过来放。该怎么办?

网上搜了一堆东西出来。安卓的资料比较多一点,iOS 的就很少了。最终解决了这个问题。大概代码如下:


uses {$IFDEF IOS}iOSapi.UIKit{$ENDIF};

-----------------------------


procedure LockOrientations_iOS(ATo: TScreenOrientation; APossibles: TScreenOrientations);
{$IFDEF IOS}
var
  win : UIWindow;
  App : UIApplication;
  new : UIViewController;
  old : UIViewController;
  toio: UIInterfaceOrientation;
{$ENDIF}
begin
{$IFDEF IOS}
  Application.FormFactor.Orientations := [];
  App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
  win := TUIWindow.Wrap(App.windows.objectAtIndex(0));
  case ATo of
    TScreenOrientation.Portrait: toio := UIInterfaceOrientationPortrait;
    TScreenOrientation.Landscape: toio := UIInterfaceOrientationLandscapeLeft;
    TScreenOrientation.InvertedLandscape: toio := UIInterfaceOrientationLandscapeRight;
    else //TScreenOrientation.InvertedPortrait
      toio := UIInterfaceOrientationPortraitUpsideDown;
  end;
  App.setStatusBarOrientation(toio);
  Application.FormFactor.Orientations := APossibles;
  new := TUIViewController.Wrap(TUIViewController.alloc.init);
  old := win.rootViewController;
  Win.setRootViewController(new);
  Win.makeKeyAndVisible;
  win.setRootViewController(old);
  win.makeKeyAndVisible;
{$ENDIF}
end;

----------------------

function ChangeScreenOrientation(const AIsLandscap: Boolean = true): Integer;

function ChangeScreenOrientation(const AIsLandscap: Boolean = true): Integer;
var
  ScreenService: IFMXScreenService;
  OrientSet: TScreenOrientations;
{$IFDEF IOS}
  screenSet: TScreenOrientation;
{$ENDIF}
begin
{$IFDEF ANDROID}
  //强制横屏。这段代码对安卓有用。网上看到的说法是对 iOS 无用。需要对 iOS 进行测试。pcplayer
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) then
  begin
    if AIsLandscap then //横屏显示
      OrientSet := [TScreenOrientation.soLandscape]
    else
      OrientSet := [TScreenOrientation.Portrait];
    ScreenService.SetScreenOrientation(OrientSet);
  end;
{$ENDIF}

{$IFDEF IOS}
  if AIsLandscap then //横屏显示
  begin
    OrientSet := [TScreenOrientation.soLandscape];
    screenSet := TScreenOrientation.Landscape;
  end
  else
  begin
    OrientSet := [TScreenOrientation.Portrait];
    screenSet := TScreenOrientation.Portrait;
  end;
  LockOrientations_iOS(screenSet, OrientSet);
{$ENDIF}
end;


使用:程序调用 ChangeScreenOrientation 这个方法,可以设置横屏或竖屏。iOS 和 Android 都可以。

相关阅读 >>

Delphi 检查注册表键、键值是否存在

Delphi xp 之后, Delphi 动注册表不方便了...逼出来一个办法:

Delphi xe5实现按android的back键无法退出关闭程序

Delphi最简单的多线程网页采集

Delphi 按字母排序的windows窗口公用消息

Delphi webbrowser1提取网页中的所有链接、点击第 n 个链接

Delphi 打包文件到apk安装包中

Delphi 用hook实现dll注入详解

Delphi重写一个字符串分割函数

Delphi listview高速添加数据

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...