简体   繁体   中英

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

<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? 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] Upside Down

[ ] Landscape Left

[ ] Landscape Right

Swift 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.

  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.

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.

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

2) Check your deployment information and select your chosen device orientations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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