On my camera view, I want to keep the orientation as portrait because when it's in landscape, the camera preview does not rotate:
struct CameraPreview: UIViewRepresentable {
@ObservedObject var camera: CameraModel
func makeUIView(context: Context) -> some UIView {
let view = UIView(frame: UIScreen.main.bounds)
camera.preview = AVCaptureVideoPreviewLayer(session: camera.session)
//...
camera.preview.videoGravity = .resizeAspectFill
camera.preview.frame = view.frame
view.layer.addSublayer(camera.preview)
// starting session
camera.session.startRunning()
return view
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
}
I know I'll need to fix this sooner or later but I need to get moving. For now, I'll prevent all other orientations except:
struct CameraView: View {
init() {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
[..]
By doing the above, when put in landscape, the camera preview cuts in half then the init "kicks in" which sets it back CameraView back into portrait. How to let it stay in portrait mode without any animation?
Placing the code inside .onAppear does nothing:
struct CameraView: View {
var body: some View {
VStack{..}
.onAppear(perform: {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
}