     }()
     
     lazy var captionBackgroundView: UIView = {
-        let y = view.bounds.height - 135
-        let frame = CGRect(x: 0, y: y, width: view.bounds.width, height: view.bounds.height)
-        let captionBackgroundView = UIView(frame: frame)
+        var captionBackgroundView = UIView()
+        if let captionHeight = captionHeight {
+            var y = view.bounds.height - captionHeight - 32.0
+            var height = captionHeight + 32.0
+            if #available(iOS 11.0, *), let window = UIApplication.shared.keyWindow {
+                let bottomSafeInsetSpacing = window.safeAreaInsets.bottom
+                height += bottomSafeInsetSpacing
+                y -= bottomSafeInsetSpacing
+            }
+            let frame = CGRect(x: 0, y: y, width: view.bounds.width, height: height)
+            captionBackgroundView = UIView(frame: frame)
+        }
         captionBackgroundView.backgroundColor = captionBackgroundViewColor
         return captionBackgroundView
     }()

            UIView.animate(withDuration: 0.05, delay: 0.0, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in
                self.closeButton.alpha = 0.0
                self.captionLabel.alpha = 0.0
                self.captionBackgroundView.alpha = 0.0
                if self.visiblePageControl {
                    self.pageControl.alpha = 0.0
                }

                    options: .curveLinear,
                    animations: { () -> Void in
                        self.captionLabel.alpha = 0.0
                        self.captionBackgroundView.backgroundColor = .clear
                }, completion: { _ -> Void in
                    self.captionLabel.text = photo.caption
                    UIView.animate(withDuration: 0.1, delay: 0.0, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in
                        self.captionLabel.alpha = 1.0
                        if let captionText = self.captionLabel.text, !captionText.isEmpty {
                            self.captionBackgroundView.backgroundColor = self.captionBackgroundViewColor
                        }
                    }, completion: nil)
                })
