如何只为一个视图控制器锁定方向?

How to lock orientation just for one view controller?

我可以在一个控制器中设置纵向和锁定方向吗?

我试过了:

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

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

但这对我没有帮助。我还必须添加其他内容吗?

此代码应该有效:

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

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

    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }

如果它现在对你有用,那么我想你的控制器在另一个控制器中(UINavigationControllerUITabBarControllerUISplitViewController)。在这种情况下,您需要在该父控制器中使用此代码。

如果您的导航控制器包含多个视图控制器,并且您只需要为其中的一些视图控制器禁用方向,那么您需要继承 UINavigationController class 并在其中编写如下内容:

class NavigationController: UINavigationController {

    var shouldRotate: Bool = true

    override func supportedInterfaceOrientations() -> Int {
        return shouldRotate ? Int(UIInterfaceOrientationMask.Portrait.rawValue) : Int(UIInterfaceOrientationMask.All.rawValue)
    }

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

然后在您需要禁用方向的控制器中,您可以为您的导航控制器禁用它:

class ViewController: UIViewController {

    var lastControllerRotationStatus: Bool?

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if let navigationController = self.navigationController as? NavigationController {
            lastControllerRotationStatus = navigationController.shouldRotate
            navigationController.shouldRotate = false
        }
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)

        if let navigationController = self.navigationController as? NavigationController {
            navigationController.shouldRotate = lastControllerRotationStatus ?? true
        }
    }
}

但请记住,在您的控制器被推出导航控制器后,您需要恢复旧的旋转状态。在此示例中,我在更改旋转状态之前保存旋转状态,并在控制器消失后恢复它。您也可以使用其他一些方法。例如,您可以重载 UINavigationController 方法 popViewController,然后将 shouldRotate 设置为 false。

与控制器变量设置相同的方法,您可以使用 AppDelegate 方法 application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int

控制旋转

那么就不需要继承navigation controller了

您的 AppDelegate class 代码将如下所示:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var shouldRotate = true
    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {

        return shouldRotate ? Int(UIInterfaceOrientationMask.All.rawValue) : Int(UIInterfaceOrientationMask.Portrait.rawValue)

    }

}

您的控制器代码将如下所示:

class ViewController: UIViewController {

    var lastControllerRotationStatus: Bool?

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
            lastControllerRotationStatus = appDelegate.shouldRotate
            appDelegate.shouldRotate = false
        }
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)

        if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
            appDelegate.shouldRotate = lastControllerRotationStatus ?? true
        }
    }
}

只需将其放入 viewDidAppear:

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

我在类似的问题上发现了这个。

“首先,我非常感谢 zellb,他给了我这个答案并让我 understand.In 命令将一个视图控制器的设备方向设置为纵向,其他视图控制器都设置为两个,您可以将其设置为AppDelegate.swift 具有 supportedInterfaceOrientationsForWindow 的此功能

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

return checkOrientation(self.window?.rootViewController?)// This is the custom function that u need to set your custom view to each orientation which u want to lock

}

这里是检查每个视图控制器的功能,以根据您的需要设置纵向或所有方向。

func checkOrientation(viewController:UIViewController?)-> Int{

if(viewController == nil){

    return Int(UIInterfaceOrientationMask.All.rawValue)//All means all orientation

}else if (viewController is SignInViewController){

    return Int(UIInterfaceOrientationMask.Portrait.rawValue)//This is sign in view controller that i only want to set this to portrait mode only

}else{

    return checkOrientation(viewController!.presentedViewController?)
}
}

并且不要忘记在属性检查器中设置您想锁定的视图控制器,其中包括“纵向”方向和其他方向“推断”

希望对您有所帮助。

如果你想要纵向但只想锁定一个viewController(例如横向),你可以这样做:

这对我有用 Swift 2.0

AppDelegate

var shouldRotate = true

//MARK: - Func to rotate only one view controller
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {

    if (shouldRotate == true){
        return UIInterfaceOrientationMask.All
    }

    return UIInterfaceOrientationMask.Landscape

}

ViewController

    override func viewDidLoad() {
        super.viewDidLoad()

        // 1. lock the rotation
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldRotate = false

        // 2. Force the device in landscape mode when the view controller gets loaded
        UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeRight.rawValue, forKey: "orientation")
    }

    override func shouldAutorotate() -> Bool {
        // 3. Lock autorotate
        return false
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [UIInterfaceOrientationMask.LandscapeRight, UIInterfaceOrientationMask.LandscapeLeft]
    }


    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {

        // 4. Only allow Landscape
        return UIInterfaceOrientation.LandscapeRight
    }

    // 5. Restoring the locked-rotation before controller disappeared
    override func viewWillDisappear(animated: Bool) {
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldRotate = true
    }

Swift3个答案:

在您想要锁定方向的视图控制器中

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}

改编自how to lock portrait orientation for only main view using swift

**添加到您的 AppDelegate 文件:*

var orientationLock = UIInterfaceOrientationMask.all
  func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return self.orientationLock
  }

  struct AppUtility {
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
      if let delegate = UIApplication.shared.delegate as? AppDelegate {
        delegate.orientationLock = orientation
      }
    }

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
      self.lockOrientation(orientation)
      UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }
  }

添加到您要强制定向的 viewcontroller:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    AppDelegate.AppUtility.lockOrientation(.portrait)
  }

  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    AppDelegate.AppUtility.lockOrientation(.all)
  }