增强版——Firemonkey运行时更改锁定iOS显示方向
2016-03-12 18:27
344 查看
之前在这里介绍过Firemonkey如何在运行时更改和锁定iOS设备的显示方向。这里给出一个改良优化的版本,增加考虑原先支持显示方向以减少不必要的重绘。
下载代码
下载代码
unit TU2.iOS.Helper; interface uses FMX.Types; //更改屏幕方向 procedure ChangeOrientations(APreferred: TScreenOrientation; ASupported: TScreenOrientations); implementation uses iOSapi.UIKit, FMX.Platform, FMX.Forms; function GetOrientation(AOrientation: UIInterfaceOrientation): TScreenOrientation; overload; begin case AOrientation of UIInterfaceOrientationPortrait: Result := TScreenOrientation.Portrait; UIInterfaceOrientationLandscapeLeft: Result := TScreenOrientation.Landscape; UIInterfaceOrientationLandscapeRight: Result := TScreenOrientation.InvertedLandscape; else Result := TScreenOrientation.InvertedPortrait; end; end; function GetOrientation(AOrientation: TScreenOrientation): UIInterfaceOrientation; overload; begin case AOrientation of TScreenOrientation.Portrait: Result := UIInterfaceOrientationPortrait; TScreenOrientation.Landscape: Result := UIInterfaceOrientationLandscapeLeft; TScreenOrientation.InvertedLandscape: Result := UIInterfaceOrientationLandscapeRight; else //TScreenOrientation.InvertedPortrait Result := UIInterfaceOrientationPortraitUpsideDown; end; end; procedure ChangeOrientations(APreferred: TScreenOrientation; ASupported: TScreenOrientations); var App : UIApplication; win : UIWindow; new,old : UIViewController; begin Include(ASupported,APreferred); App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication); if not (GetOrientation(App.statusBarOrientation) in ASupported) then App.setStatusBarOrientation(GetOrientation(APreferred));//Change stausbar orientations //Change Supported orientations if Application.FormFactor.Orientations<>ASupported then begin //The first Windows is always the main Window win := TUIWindow.Wrap(App.windows.objectAtIndex(0)); //we store all our current content to the old ViewController old := win.rootViewController; {Now we are creating a new(dummy) Viewcontroller, after it is created, it will have to check what is the supported orientations} Application.FormFactor.Orientations := ASupported; new := TUIViewController.Wrap(TUIViewController.alloc.init); Win.setRootViewController(new); Win.makeKeyAndVisible;// We display the Dummy viewcontroller {And now we Display our original Content in a new Viewcontroller with our new Supported orientations} win.setRootViewController(old); win.makeKeyAndVisible; end; end; end.
相关文章推荐
- ios layout相关方法
- iOS 图片编辑——涂鸦
- iOS runtime 运行时( 二 深谈)
- iOS导航栏配置问题
- iOS 火星坐标相关整理及解决方案汇总
- Firemonkey访问iOS的钥匙串Keychain
- iOS runtime 运行时( - 俗谈)
- iOS GCD使用
- nil、Nil、NULL和NSNull区别
- cookie
- 获取IOS屏幕尺寸大小
- iOS 版本更新
- iOS页面跳转及数据传递
- weakify和strongify
- iOS9中如何注册远程通知
- iOS9中如何注册远程通知
- iOS9中如何注册远程通知
- iOS 时间,日期,字符串的处理
- SVProgressHUD使用详解
- 深入理解iOS开发中的BitCode功能