【问题标题】:Trying to prevent orientation change on an iPhone 4试图阻止 iPhone 4 上的方向更改
【发布时间】:2018-09-04 09:56:32
【问题描述】:

我在 iOS 应用程序中遇到了旋转(方向更改)问题。 该应用程序有几个视图控制器可供导航。

我希望它可以旋转,但 iPhone 4(3.5 英寸设备)除外。 在 3.5 英寸设备上,它应该始终保持纵向模式。

我能够防止直接旋转,即在视图控制器内部时,如果用户将设备保持在纵向模式并且它们处于横向模式,则不会发生任何事情。这正是我想要的。

在横向模式下更改视图控制器时会出现问题。 在这种情况下,新的视图控制器以横向模式显示,我不希望这样。

我尝试使用

- (BOOL)shouldAutorotate;

当设备为 3.5 英寸但不起作用时返回 NO。

如何防止这种行为?

我可能需要添加一个细节: 我用来测试的 3.5 英寸设备运行 iOS 版本 9.3.5。

我使用的是 Xcode 9.4.1。

【问题讨论】:

    标签: ios objective-c iphone-4 orientation-changes shouldautorotate


    【解决方案1】:

    你可以使用supportedInterfaceOrientations的方法。

    例如:

    目标 c

    -(UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        //return your preferred orientation 
        return UIInterfaceOrientationMaskPortrait;
    }
    

    【讨论】:

    • 准确地说,我需要做一个小修正,但你给了我前进的道路。我修复了你的代码,所以如果其他人看到这篇文章,它就可以工作。
    【解决方案2】:

    您需要在 AppDelegate.m 中添加此代码。 iPhone 4/ iPhone 4s 的屏幕高度为 480 点。

    Objective-C

    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
    {
        if(self.window.bounds.size.height > 500) {
            return (UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait);
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

    您可能还需要在 Target 的 Deployment Info 中启用 Require Full Screen

    【讨论】:

    • 在我的情况下这似乎没有必要。
    • 好吧,没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多