How to lock orientation of one view controller to portrait mode only in Swift

后端 未结 16 2209
梦谈多话
梦谈多话 2020-11-22 12:07

Since my app got support for all orientation. I would like to lock only portrait mode to specific UIViewController.

e.g. assume it was Tabbed Application and when Si

16条回答
  •  天涯浪人
    2020-11-22 12:18

    bmjohns -> You are my life saviour. That is the only working solution (With the AppUtility struct)

    I've created this class:

    class Helper{
        struct AppUtility {
    
            static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
    
                if let delegate = UIApplication.shared.delegate as? AppDelegate {
                    delegate.orientationLock = orientation
                }
            }
    
            /// OPTIONAL Added method to adjust lock and rotate to the desired orientation
            static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
    
                self.lockOrientation(orientation)
    
                UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
            }
    
        }
    }
    

    and followed your instructions, and everything works perfectly for Swift 3 -> xcode version 8.2.1

提交回复
热议问题