简体   繁体   中英

How can I lock camera view orientation to landscape in iOS?

I'd like to fix my Camera View in landscape Mode. And also show an alert when user rotate view to portrait. is there any solution available ?

Put this in your view controller:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape {
        print("will turn to landscape")
    } else {
        print("will turn to portrait")
        //print an alert box here
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeLe‌​ft.rawValue, forKey: "orientation")
    }
}

Source: How to detect orientation change?

Or put this to the view that needs to be in landscape:

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

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscape
}

But this one cannot post an alert.

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