 
 @objc(EmbeddedBarcodeReader) class EmbeddedBarcodeReader: CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
     
-    class CameraPreview: UIViewController {
+    class CameraPreview: UIView {
         var videoPreviewLayer:AVCaptureVideoPreviewLayer?
         var qrCodeFrameView:UIView?
         
-        override func viewDidLoad() {
-            super.viewDidLoad()
+        func interfaceOrientationToVideoOrientation(_ orientation : UIInterfaceOrientation) -> AVCaptureVideoOrientation {
+            switch (orientation) {
+            case UIInterfaceOrientation.portrait:
+                return AVCaptureVideoOrientation.portrait;
+            case UIInterfaceOrientation.portraitUpsideDown:
+                return AVCaptureVideoOrientation.portraitUpsideDown;
+            case UIInterfaceOrientation.landscapeLeft:
+                return AVCaptureVideoOrientation.landscapeLeft;
+            case UIInterfaceOrientation.landscapeRight:
+                return AVCaptureVideoOrientation.landscapeRight;
+            default:
+                return AVCaptureVideoOrientation.portraitUpsideDown;
+            }
+        }
+        
+        override func layoutSubviews() {
+            super.layoutSubviews();
+            if let sublayers = self.layer.sublayers {
+                for layer in sublayers {
+                    layer.frame = self.bounds;
+                }
+            }
+            
+            self.videoPreviewLayer?.connection.videoOrientation = interfaceOrientationToVideoOrientation(UIApplication.shared.statusBarOrientation);
+        }
+        
+        func addPreviewLayer() {
+            videoPreviewLayer!.frame = self.bounds
+            self.backgroundColor = UIColor.clear
+            self.layer.addSublayer(videoPreviewLayer!)
+        }
+        
+        func removePreviewLayer() {
+            self.videoPreviewLayer!.removeFromSuperlayer()
+            self.videoPreviewLayer = nil
         }
     }
     

            if metadataObj.stringValue != nil {
                if (self.barcodeReadCallback != nil) {
                    let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: metadataObj.stringValue)
                    commandDelegate!.send(pluginResult, callbackId:self.barcodeReadCallback)
                }
            }

    var width:CGFloat = 0.0
    var height:CGFloat = 0.0
    
    override func pluginInitialize() {
        super.pluginInitialize()
        self.webView!.backgroundColor = UIColor.clear
        self.webView!.isOpaque = false
    }
    
    func startReading(_ command: CDVInvokedUrlCommand) {
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        do {
