简体   繁体   English

自动旋转代码适用于iPhone,但不适用于iPad

[英]Auto rotating code works on iPhone but not on iPad

This is the code that only works on the iphone. 这是仅适用于iPhone的代码。 I am making a universal app that runs on both devices, so it it the identical app. 我正在制作可以在两种设备上运行的通用应用程序,因此它是相同的应用程序。

**//Automatically rotates the view to landscape
    let orientation: UIDeviceOrientation = UIDevice.current.orientation
    //If landscape right the rotate right
    if orientation == UIDeviceOrientation.landscapeLeft
    {
        UIView.animate(withDuration: 0.5, animations:
            {
                () -> Void in
                let value = UIInterfaceOrientation.landscapeRight.rawValue
                UIDevice.current.setValue(value, forKey: "orientation")
        })


    }
    else
    {
        UIView.animate(withDuration: 0.5, animations:
            {
                () -> Void in
                let value = UIInterfaceOrientation.landscapeLeft.rawValue
                UIDevice.current.setValue(value, forKey: "orientation")
        })

    }**

Here's one possibility for you to check... 这是您检查的一种可能性...

In the project's settings, under the General tab for a build target, there's the Deployment Info section where you select the "Devices" option (that you probably have on "Universal"). 在项目设置的构建目标的“常规”选项卡下,有“部署信息”部分,您可以在其中选择“设备”选项(可能在“通用”选项中)。 Below that is the Device Orientation selection. 在此下方是设备方向选择。 There's one, um, interesting feature here. 这里有一个有趣的功能。

If you check some orientations when Devices is on Universal, it only applies the setting to the iPhone version. 如果在“设备”位于“通用”上时检查某些方向,则仅将设置应用于iPhone版本。 If you toggle the Devices selection to iPad, the options will likely be different. 如果将“设备”选择切换到iPad,则选项可能会有所不同。 What you need to do is switch the selection to iPhone, apply the orientations you want there, switch it to iPad, apply orientations again, and then finally set Devices to Universal. 您需要做的是将选择切换到iPhone,在此处应用所需的方向,将其切换到iPad,再次应用方向,最后将“设备”设置为“通用”。

I have a hunch (not tested with your code) that if you try to apply an orientation that is not supported here, your code will do nothing. 我有一种直觉(未经您的代码测试),如果您尝试应用此处不支持的方向,则您的代码将无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM