简体   繁体   English

使用Swift在iPhone应用程序中的上下颠倒方向

[英]Upside down orientation in iPhone app using Swift

I am developing an app and intend to use upside down orientation in it. 我正在开发一个应用程序,并打算在其中使用颠倒方向。 I have used the following code 我使用了以下代码

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
        if toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown {
            self.shouldAutorotate()
        }
    }

But its not working. 但是它不起作用。 Any help!??? 任何帮助!

Try to put this on your plist file 尝试将其放在您的plist文件中

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>

And also verify on your target on general tab on Deployment info to have this: 并在“ 部署”信息的“ 常规”选项卡上验证您的目标是否具有此功能:

在此处输入图片说明

On the other hand, are you using Navigation controller or Tab controller? 另一方面,您使用的是导航控制器还是Tab控制器? If so, you will need to subclass navigation or tab controllers and add these two methods: 如果是这样,您将需要对导航或选项卡控制器进行子类化,并添加以下两种方法:

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

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.All
    }

Device orientation (General Tab): 设备方向(“常规”标签):

[x] Portrait [x]肖像

[x] Upside Down [x]颠倒

[ ] Landscape Left []风景左

[ ] Landscape Right []景观权

Swift 2 迅捷2

// UINavigationController+UpsideDownOrientation.swift

import UIKit
extension UINavigationController {

    public override func shouldAutorotate() -> Bool {
        return true
    }

    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

        return [.Portrait, .PortraitUpsideDown]
    }
}

Add this in your view controller code. 将此添加到您的视图控制器代码中。

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

Add this code on your Class or ViewController. 将此代码添加到您的Class或ViewController上。

  override func shouldAutorotate() -> Bool {
  return false 
 }  
  override func supportedInterfaceOrientations() -> Int {
  return UIInterfaceOrientation.Portrait.rawValue
}

Add these two functions to the viewcontrollers that you want to set the orientation to. 将这两个功能添加到您要设置方向的视图控制器。 Works for Swift 3.0 because I'm using it right now. 适用于Swift 3.0,因为我现在正在使用它。

On your project, under General, select the "Device Orientation" that you want the App to support. 在项目的“常规”下,选择要应用程序支持的“设备方向”。 Then go to the .plist and edit the Supported interface orientations (iPad) for the orientation you want to support. 然后转到.plist并编辑要支持的方向的“支持的界面方向(iPad)”。

override var shouldAutorotate: Bool {
        return true
    }

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeRight // or .landscapeLeft or .portrait or .portraitUpsideDown which ever orientation you want the viewcontroller to be

} }

1) Check your info.plist for Supported interface orientations, set it as you wish 1)检查您的info.plist以获取支持的界面方向,并根据需要进行设置

2) Check your deployment information and select your chosen device orientations 2)检查您的部署信息并选择您选择的设备方向

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

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