简体   繁体   中英

viewWillTransitionToSize for locked orientation

Is there any way to detect the screen orientation even when the user has locked his orientation? My App uses a custom camera view and I would like to be able to save images in either landscape or portrait depending on the orientation. I am currently using the following code to detect the orientation, however this only works if the user has not locked it.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        let orientation: UIDeviceOrientation = UIDevice.current.orientation

        switch (orientation) {
        case .portrait:
            print("Portrait")
        case .landscapeRight:
            print("Left")
        case .landscapeLeft:
            print("Right")
        default:break
        }
    }

If this is not the correct way to work with the camera orientation, please guide me in the right direction.

I believe that the only way to do this is enabling both portrait and landscape mode in project: 在此处输入图片说明

Then, override these properties in all your view controllers that don't need a landscape mode.

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}

In the view controller with embedded camera just set shouldAutorotate false and add extra supportedInterfaceOrientations that are used in landscape.

Note that if you use UINavigationController or UITabBarController it will not work, so you should create these extensions: https://stackoverflow.com/a/39674618/5381663

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM