简体   繁体   English

如何在iOS中将相机视图方向锁定为横向?

[英]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. 但这不能发出警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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