【问题标题】:locking orientation does not work on ipad ios 8 swift xcode 6.2锁定方向在 ipad ios 8 swift xcode 6.2 上不起作用
【发布时间】:2015-08-06 09:35:15
【问题描述】:

我像这样将应用程序方向从一般应用程序设置锁定为纵向

但是当我在 ipad 上运行我的应用程序时,它还是会旋转!?

那么我应该在哪里锁定方向?

为什么苹果在设置中添加了这个功能,而它却不起作用!?

更新:

我也尝试将此 viewController 实现为所有 viewController 的通用。

import Foundation
import UIKit

class GeneralViewController: UIViewController {

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        let value = UIInterfaceOrientation.Portrait.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
    }

    override func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }


    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }

    override func shouldAutorotate() -> Bool {
        return false
    }
}

这张图片取自我应用的主导航控制器。

还是不行!

这东西到底是怎么工作的?

【问题讨论】:

    标签: ios iphone swift


    【解决方案1】:

    您可以覆盖您的视图控制器属性shouldAutorotate

    override var shouldAutorotate: Bool {
        return false 
    }
    

    如果您的视图控制器嵌入在导航控制器中,您可以创建自定义导航控制器:

    class PortraitNavigationController: UINavigationController {
    
        override var shouldAutorotate: Bool {
            return false 
        }
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
        }
        */
    }
    

    【讨论】:

    • 您可以创建一个自定义视图控制器并添加到那里
    • 啊,听起来好多了,我正在检查像代理外观这样的解决方案来更改它一次,它会在整个项目中生效
    • 是嵌入到 Navigation 控制器还是 TabBar 控制器中?
    • 是的,我在某些控制器中使用导航控制器和 tabBar
    • 尝试将支持的界面方向添加到您的导航控制器
    【解决方案2】:

    您不需要额外的代码,我正在开发 Xcode 7.0.1,我注意到当我将屏幕方向锁定为仅纵向时,它仅锁定在 iPhone 中,也锁定在 iPad 中,您需要选择iPad从设备(而不是通用)并检查纵向并取消选中其他,然后再次将设备返回通用,因为我发现通用只反映在iPhone中,它可能是Xcode中的某种错误。

    【讨论】:

    • 绝对好的答案。我们怎么会知道呢? :D。这是一个很好的发现。
    • 我对此赞不绝口。我正在努力解决这个问题。
    • 即使在 Xcode 9 中仍然存在同样的困惑!在通用工作之前检查每个设备方向:) tnx
    • 我确实尝试了 iOS 11 中的所有内容,这救了我。不是很明显的解决办法,谢谢分享!!!
    • 不敢相信到目前为止它仍然是一个错误(在 Xcode 10.1 上测试)
    【解决方案3】:

    我也有同样的问题。对我有用的是->在设备类型中选择iPad而不是通用->取消选中除纵向之外的其他三个选项->再次选择通用。完成。

    【讨论】:

    • 你拯救了我的一天!
    【解决方案4】:

    这将是添加到 Mohammed Elrashidy 解决方案的评论,但我没有制作 cmets 的声誉。我正在使用 XCode 9.1 和 Swift 3。

    我的应用需要一些图形屏幕来自动旋转,但所有其他屏幕都保持固定。我的问题是 General > Deployment Info 中的“iPad”设备配置与 iPhone 和 Universal 设备的配置不一致。我检查了 iPhone 和 Universal 的“纵向”、“横向左侧”和“横向右侧”,但检查了 iPad 的所有方向(包括“倒置”)。简单地取消选中“Upside Down”会导致 iPad 设备遵守我的 Swift 逻辑中的 shouldAutorotate 和 supportedInterfaceOrientations 调用。

    我的 Info.plist 现在如下所示,我可以通过编程方式为 iPad 打开或关闭自动旋转。

    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    

    【讨论】:

    • 完美解决方案,谢谢!在 plist 中设置它很可能是更好的选择
    【解决方案5】:

    在按照上述解决方案解决问题后,我只是想添加您需要做的其他事情:

    在 XCode 中,确保您还选中了 General > Targets 下的“Requires Full Screen”复选框。

    参考: iPad Multitasking support requires these orientations

    【讨论】:

    • 被低估的评论,在尝试了 10 多个答案之后,这是最终奏效的。还需要向 ViewController 添加代码。 XCode 12.
    【解决方案6】:

    由于 XCode 在这 5 年中发生了很大变化,当我遇到同样的问题时,我找到了一种简单的方法来处理它,即使我将设备方向配置为只允许“纵向”,限制在 iPad 中不起作用设备。

    所以在新创建的项目中,需要进入Targets->Info->Custom iOS Target Properties->Supported interface orientations(iPad)。然后删除你不需要的布局,然后这个技巧就会奏效。

    【讨论】:

      【解决方案7】:

      打开 Info.plist,在“支持的界面方向 (iPad)”下删除您不需要的项目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-24
        • 2020-12-24
        • 1970-01-01
        相关资源
        最近更新 更多