【问题标题】:How to lock orientation just for one view controller?如何仅为一个视图控制器锁定方向?
【发布时间】:2015-04-29 17:21:45
【问题描述】:

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

我试过了:

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

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

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

【问题讨论】:

    标签: ios swift orientation portrait


    【解决方案1】:

    这段代码应该可以工作:

        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 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控制旋转

    那么你就不需要从导航控制器继承了。

    您的 AppDelegate 类代码如下所示:

    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
            }
        }
    }
    

    【讨论】:

    • 它不再起作用了。我的 viewController 在 NavigationController 中,但它(NavigationController)还有许多其他 viewController,我只需要在一个控制器中使用它
    • @OrkhanAlizade 已回答
    • NavigationController 错误Use of undeclarated type NavigationController
    • @OrkhanAlizade 你需要继承 UINavigationController。 NavigationController 类继承自 UINavigationController
    • 喜欢这个class InfoController: UIViewController, UINavigationController 还是?这又不行了
    【解决方案2】:

    如果你想要纵向但你只想锁定一个视图控制器(例如在横向)你可以这样做:

    这对我来说适用于 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
    
    }
    

    视图控制器

        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
        }
    

    【讨论】:

      【解决方案3】:

      **添加到您的 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")
          }
        }
      

      添加到要强制定向的视图控制器:

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

      【讨论】:

        【解决方案4】:

        我在一个类似的问题上找到了这个。 Link

        “首先,我真的很感谢 zellb,他给了我这个答案并让我明白了。为了将一个视图控制器的设备方向设置为纵向,其他的都设置为纵向,您可以在 AppDelegate.swift 中使用这个函数支持InterfaceOrientationsForWindow

        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?)
        }
        }
        

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

        希望对您有所帮助。

        【讨论】:

          【解决方案5】:

          只要把这个放在 viewDidAppear 中:

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

          【讨论】:

          • 你还留下了你的代码吗?在 shouldAutoRotate 中返回 no?
          【解决方案6】:

          斯威夫特 3 答案:

          在视图控制器中你要锁定方向

          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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多