禁用当前屏幕的旋转?

是否可以为当前屏幕启用/禁用旋转参数,或者此属性适用于所有应用程序?

当然:

- (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { //Choose your available orientation, you can also support more tipe using the symbol | //eg return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight) return (UIInterfaceOrientationMaskPortrait); } 

如果您在NavigationController中有多个ViewControllers,并且您希望仅在其中一个中禁用旋转,则需要在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 } 

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