 
         view.backgroundColor = UIColor.black
 
-        previewView = UIView(frame: view.bounds)
+        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIDeviceOrientationDidChange, object: nil, queue: nil) { (_) -> Void in
+            switch UIDevice.current.orientation {
+            case .faceDown, .faceUp, .unknown:
+                ()
+            case .landscapeLeft, .landscapeRight, .portrait, .portraitUpsideDown:
+                self.orientation = UIDevice.current.orientation
+                self.updateWidgetsToOrientation()
+            }
+        }
+    }
+
+    private func updateImagePickerButton() {
+        if imagePickerAdapter == nil {
+            if pickerButton != nil {
+                pickerButton?.removeFromSuperview()
+                pickerButton = nil
+            }
+        } else {
+            if pickerButton == nil {
+                let pickerButtonWidth: CGFloat = 114
+                pickerButton = UIButton(frame: CGRect(x: viewFrame.width - pickerButtonWidth - buttonMargin, y: viewFrame.origin.y + buttonMargin, width: pickerButtonWidth, height: 38))
+                pickerButton!.setTitle(NSLocalizedString("Photos", comment: "Select from Photos buttont itle"), for: UIControlState())
+                pickerButton!.setImage(UIImage(named: "PhotosIcon"), for: UIControlState())
+                pickerButton!.addTarget(self, action: #selector(presentImagePickerTapped(_:)), for: .touchUpInside)
+                pickerButton!.titleLabel?.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.footnote)
+                pickerButton!.autoresizingMask = [.flexibleTopMargin]
+                pickerButton!.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
+                roundifyButton(pickerButton!)
+                view.addSubview(pickerButton!)
+            }
+        }
+    }
+
+    open override func viewWillAppear(_ animated: Bool) {
+        super.viewWillAppear(animated)
+
+        // In case the application uses the old style for managing status bar appearance
+        UIApplication.shared.setStatusBarHidden(true, with: .slide)
+    }
+
+    open override func viewDidAppear(_ animated: Bool) {
+        super.viewDidAppear(animated)
+        if #available(iOS 11.0, *) {
+            view.insetsLayoutMarginsFromSafeArea = true
+            viewFrame = view.convert(view.safeAreaLayoutGuide.layoutFrame, to: view.superview ?? view)
+            viewBounds = view.safeAreaLayoutGuide.layoutFrame
+        } else {
+            viewFrame = view.frame
+            viewBounds = view.bounds
+        }
+
+        setupSubviews()
+
+        collectionView.reloadData()
+        scrollToLastAddedAssetAnimated(false)
+    }
+
+    func setupSubviews() {
+        // Only create subviews once in the views lifecycle.
+        // Views need to be added during viewDidAppear for the iPhone X's safeAreas to be known.
+        if subviewsCreated { return }
+        subviewsCreated = true
+
+        previewView = UIView(frame: viewBounds)
         previewView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
         view.addSubview(previewView)
         let previewLayer = captureManager.previewLayer
         // We are using AVCaptureSessionPresetPhoto which has a 4:3 aspect ratio
-        let viewFinderWidth = view.bounds.size.width
+        let viewFinderWidth = viewBounds.size.width
         var viewFinderHeight = (viewFinderWidth / 3) * 4
         if captureManager.viewfinderMode == .fullScreen {
-            viewFinderHeight = view.bounds.size.height
+            viewFinderHeight = viewBounds.size.height
         }
         previewLayer.frame = CGRect(x: 0, y: 0, width: viewFinderWidth, height: viewFinderHeight)
         previewView.layer.addSublayer(previewLayer)

        captureButton.isEnabled = false
        captureButton.accessibilityLabel = NSLocalizedString("Take a picture", comment: "")

        closeButton = UIButton(frame: CGRect(x: captureButton.frame.maxX, y: captureButton.frame.midY - 22, width: view.bounds.width - captureButton.frame.maxX, height: 44))
        closeButton.addTarget(self, action: #selector(doneButtonTapped(_:)), for: .touchUpInside)
        closeButton.setTitle(NSLocalizedString("Done", comment: ""), for: UIControlState())
        closeButton.tintColor = UIColor.white

                self.previewView.alpha = 1.0
            })
        }
    }

    open override func viewWillDisappear(_ animated: Bool) {
