Giphy / giphy-ios-sdk

Home of the GIPHY SDK iOS example app, along with iOS SDK documentation, issue tracking, & release notes.
https://developers.giphy.com/
Mozilla Public License 2.0
116 stars 52 forks source link

SwiftUI UIViewControllerRepresentable Presents On Top of White View #237

Closed noahbino closed 2 weeks ago

noahbino commented 1 year ago

I've created a SwiftUI adaption, but it presents with a white screen behind the actual GIF selector, and I cannot find out why.

IMG_0F678BC2BF9E-1 IMG_E7233012C093-1

.sheet(isPresented: $viewModel.showGIF, content: {
            GiphyPicker(selectedGifURL: $viewModel.gifURL)
                .background(Color.clear)
                .onDisappear {
                    if viewModel.gifURL == nil {
                        viewModel.selectedMedia.removeAll()
                        viewModel.selectedTypes.removeAll { $0 == .gif }
                    }
                }
        })

struct GiphyPicker: UIViewControllerRepresentable {
    @Binding var selectedGifURL: URL?

    func makeUIViewController(context: Context) -> UIViewController {
        let giphy = GiphyViewController()
        giphy.delegate = context.coordinator
        giphy.theme = .init(type: .darkBlur)
        giphy.mediaTypeConfig = [.gifs]
        giphy.modalPresentationStyle = .fullScreen
        giphy.view.backgroundColor = .clear

        let hostingController = UIViewController()
        hostingController.view.backgroundColor = .clear
        hostingController.addChild(giphy)
        hostingController.view.addSubview(giphy.view)
        giphy.view.frame = hostingController.view.bounds
        giphy.didMove(toParent: hostingController)

        return hostingController
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, GiphyDelegate {
        func didDismiss(controller: GiphyUISDK.GiphyViewController?) {

        }

        var parent: GiphyPicker

        init(_ parent: GiphyPicker) {
            self.parent = parent
        }

        func didSelectMedia(giphyViewController: GiphyViewController, media: GPHMedia) {
            parent.selectedGifURL = URL(string: media.url(rendition: .original, fileType: .gif)!)
            giphyViewController.dismiss(animated: true, completion: nil)
        }
    }
}
ALexanderLonsky commented 1 month ago

Please refer to the new SwiftUI example app