1

Is it possible to enable/disable rotation parameter for current screen or this property is for all application?

4

2 回答 2

5

当然:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    //Choose your available orientation, you can also support more tipe using the symbol |
    //e.g. return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)
    return (UIInterfaceOrientationMaskPortrait);
}
于 2014-05-05T09:13:07.387 回答
0

如果您在 NavigationController 中有多个 ViewController,并且您希望仅在其中一个中禁用旋转,则需要在 ApplicationDelegate 中设置和控制旋转。以下是如何在 Swift 中执行此操作...

在 AppDelegate.swift 中:

class AppDelegate: UIResponder, UIApplicationDelegate {


var blockRotation: Bool = false

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {

    if (self.blockRotation) {
        println("supportedInterfaceOrientations - PORTRAIT")
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        println("supportedInterfaceOrientations - ALL")
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

在要阻止旋转的 ViewController 中,将UIApplicationDelegate添加到您的类...

class LoginViewController: UIViewController, UITextFieldDelegate, UIApplicationDelegate {

然后创建对 AppDelegate 的引用...

var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

在 viewDidLoad 中,设置 appDelegate.blockRotation = true:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    appDelegate.blockRotation = true

}

在 viewWillAppear 中,设置方向以强制设备为所选方向(本例中为纵向):

override func viewWillAppear(animated: Bool) {

    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")

}

然后在 viewWillDisappear 中,或者在 prepareForSegue 中,设置 appDelegate.blockRotation = false:

override func viewWillDisappear(animated: Bool) {
    appDelegate.blockRotation = false
}

这将阻止包含多个 ViewController 的导航控制器中的一个视图控制器中的旋转。希望这可以帮助。

于 2015-04-17T15:44:04.423 回答