hyperoslo / Lightbox

:milky_way: A convenient and easy to use image viewer for your iOS app
https://www.hyper.no
Other
1.62k stars 326 forks source link

Presenting LightBoxController doesn't work. #282

Open iamimsh opened 2 years ago

iamimsh commented 2 years ago

I've updated to the latest version, I saw in previous version that this issue was resolved. However, it isn't solved yet. App freezes when presenting LightBoxController.

lexonli commented 2 years ago

Don't know if we have the same problem, but when trying to present Lightbox from a .pageSheet modalPresentationStyle (the default style now in iOS 13) I get a transparent screen that blocks anything from happening.

My solution (workaround) was to just start an empty view controller on .fullScreen and then present lightbox from there

Here's a helper class, maybe it helps:

class FullScreenImageViewController: UIViewController {

    private var controller: LightboxController?
    private var hasDisplayed = false

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        guard let controller = controller else {
            return
        }
        if hasDisplayed {
            dismiss(animated: false, completion: nil)
        } else {
            hasDisplayed = true
            controller.modalPresentationStyle = .fullScreen
            controller.dynamicBackground = true
            present(controller, animated: true)
        }
    }

    func configure(image: UIImage) {
        controller = LightboxController(images: [.init(image: image)])
    }

    func configure(imageURL: URL) {
        controller = LightboxController(images: [.init(imageURL: imageURL)])
    }
}

Usage

let fullScreenImageScene = FullScreenImageViewController()
fullScreenImageScene.modalPresentationStyle = .fullScreen
fullScreenImageScene.configure(image: myImage)
present(fullScreenImageScene, animated: false)