iOS presentedViewController 单屏幕旋转整理

    xiaoxiao2021-12-14  21

    关联: iOS presentedViewController 单屏幕旋转整理

    参考资料: iOS屏幕旋转二三事(Orientations) 效果图:

    使用过多种单屏幕旋转的方式均宣告失败,要么是代码过于复杂臃肿,要么是实现方式欠佳,甚至根本无法实现或者直接崩溃。

    经过多次多次尝试,暂时使用 模态窗口 实现单屏幕自动旋转,Push 的那种方式实现单屏幕旋转还有待考证,至今没找到逻辑思路清晰的资料(更有甚者居然要修改全部 Controller 文件,我有一句妈卖批不知当讲不当讲)。

    在项目只有一种显示方向的时候,我们要实现单屏幕旋转简单粗暴的方式就是旋转 view 了,修改 frame 让用户感官上觉的是横屏的界面。

    但是会有这样的问题:

    大概是这样婶儿的

    说正事儿:

    ①首先配置工程如下(这步等价于在 info.plist 写入支持方向,必写!不然崩溃)

    ②AppDelegate.m 添加代码如下

    #import "YourViewController.h" - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { //获取当前展示的最顶层 rootViewController UIViewController *rootController = self.window.rootViewController; if ([rootController.presentedViewController isKindOfClass:[YourViewController class]]) { YourViewController *yourViewVC = (YourViewController *)rootController.presentedViewController; if (yourViewVC.isPresented) { return UIInterfaceOrientationMaskPortrait;//竖屏 }else{ return UIInterfaceOrientationMaskLandscape;//左横 右横 } } else { return UIInterfaceOrientationMaskPortrait;//竖屏 } }

    ③YourViewController.h 添加代码如下

    //记录当前控制器是否做了退出操作 @property (nonatomic, assign) BOOL isPresented;

    YourViewController.m 添加代码如下

    - (BOOL)shouldAutorotate { //进入当前页面应该返回 YES ,当退出后这里应该置 NO return !_isPresented; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { //本页面 支持方向 return UIInterfaceOrientationMaskLandscape;//左横和右横 } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { //进入页面 默认支持方向 return UIInterfaceOrientationLandscapeRight;//右横 } - (void)dismiss { self.isPresented = YES; [self dismissViewControllerAnimated:YES completion:nil]; }

    挺简单的,不给提供 Demo 了。另外有谁家大神有 Push 方式的单屏幕旋转方式,不妨在评论里给上链接。思路清晰,改动文件少的。我也会不遗余力的去尝试,找到一种正常点适合 push旋转的方法。

    转载请注明原文地址: https://ju.6miu.com/read-962574.html

    最新回复(0)