         let tapper = UITapGestureRecognizer(target: self, action: #selector(focusTapGestureRecognized(_:)))
         previewView.addGestureRecognizer(tapper)
 
-        var collectionViewHeight: CGFloat = min(view.frame.size.height / 6, 120)
-        let collectionViewBottomMargin: CGFloat = 70
+        var collectionViewHeight: CGFloat = min(viewFrame.size.height / 6, 120)
+        let collectionViewBottomMargin: CGFloat = 0 //70
         let cameraButtonHeight: CGFloat = 66
 
-        var containerFrame = CGRect(x: 0, y: view.frame.height - collectionViewBottomMargin - collectionViewHeight, width: view.frame.width, height: collectionViewBottomMargin + collectionViewHeight)
+        var containerFrame = CGRect(x: viewFrame.origin.x, y: viewFrame.origin.y + viewBounds.height - collectionViewBottomMargin - collectionViewHeight, width: viewBounds.width, height: collectionViewBottomMargin + collectionViewHeight)
         if captureManager.viewfinderMode == .window {
-            let containerHeight = view.frame.height - viewFinderHeight
-            containerFrame.origin.y = view.frame.height - containerHeight
+            let containerHeight = viewFrame.height - viewFinderHeight
+            containerFrame.origin.y = viewFrame.origin.y + viewFrame.height - containerHeight
             containerFrame.size.height = containerHeight
             collectionViewHeight = containerHeight - cameraButtonHeight
         }

    fileprivate let buttonMargin: CGFloat = 12
    fileprivate var orientation: UIDeviceOrientation = .portrait

    deinit {
        captureManager.stop(nil)
    }


        view.backgroundColor = UIColor.black

        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 = viewBounds.size.width
        var viewFinderHeight = (viewFinderWidth / 3) * 4
        if captureManager.viewfinderMode == .fullScreen {
            viewFinderHeight = viewBounds.size.height
        }
        previewLayer.frame = CGRect(x: 0, y: 0, width: viewFinderWidth, height: viewFinderHeight)
        previewView.layer.addSublayer(previewLayer)
