         session.stopRunning()
     }
     
-    func onApplicationDidChangeStatusBarOrientation() {
+    // MARK: View lifecycle
+    
+    override func viewDidLayoutSubviews() {
+        super.viewDidLayoutSubviews()
+        
+        let videoOrientation = RSCodeReaderViewController.InterfaceOrientationToVideoOrientation(UIApplication.sharedApplication().statusBarOrientation)
         if videoPreviewLayer != nil
-            && videoPreviewLayer!.connection.supportsVideoOrientation {
-                videoPreviewLayer!.connection.videoOrientation = RSCodeReaderViewController.InterfaceOrientationToVideoOrientation(UIApplication.sharedApplication().statusBarOrientation)
+            && videoPreviewLayer!.connection.supportsVideoOrientation
+            && videoPreviewLayer!.connection.videoOrientation != videoOrientation {
+                videoPreviewLayer!.connection.videoOrientation = videoOrientation
         }
     }
-    
-    // MARK: View lifecycle
     
     override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
         super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "onApplicationWillEnterForeground", name:UIApplicationWillEnterForegroundNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "onApplicationDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "onApplicationDidChangeStatusBarOrientation", name: UIApplicationDidChangeStatusBarOrientationNotification, object: nil)
        
        session.startRunning()
    }

        
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationWillEnterForegroundNotification, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationDidEnterBackgroundNotification, object: nil)
        
        session.stopRunning()
    }
