0

I am using LinkPresentation framework in SwiftUI project for displaying rich links in a List view. The link is getting displayed but however I am not able to set (change) the frame for the LPLinkView. LPLinkView is wrapped inside an HStack and displayed in a list.


import SwiftUI
import LinkPresentation

struct ContentView: View {
    @State var redrawPreview = false
    let links: [StringLink]  = [StringLink(id: UUID(), string: "https://www.youtube.com/watch?v=HXoVSbwWUIk"),
                                 StringLink(id: UUID(), string: "https://www.youtube.com/watch?v=F2ojC6TNwws"),
                                 StringLink(id: UUID(), string: "https://www.youtube.com/watch?v=bz6GTYaIQXU")]
    var body: some View {
        List(links) { l in
            HStack {
                Image(systemName: "person.fill")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 40.0, height: 40.0)
                LinkRow(previewURL: URL(string: l.string)!, redraw: self.$redrawPreview)
            }
        }.environment(\.defaultMinListRowHeight, 50)
    }
}

struct LinkRow : UIViewRepresentable {
    
    var previewURL:URL
    @Binding var redraw: Bool
    
    func makeUIView(context: Context) -> LPLinkView {
        let view = LPLinkView(url: previewURL)
        
        let provider = LPMetadataProvider()
        provider.startFetchingMetadata(for: previewURL) { (metadata, error) in
            if let md = metadata {
                DispatchQueue.main.async {
                    view.metadata = md
                    view.sizeToFit()
                    self.redraw.toggle()
                }
            }
            else if error != nil
            {
                let md = LPLinkMetadata()
                md.title = "Custom title"
                view.metadata = md
                view.sizeToFit()
                self.redraw.toggle()
            }
        }
        
        return view
    }
    
    func updateUIView(_ view: LPLinkView, context: Context) {
        // New instance for each update
    }
}

struct StringLink : Identifiable{
    var id = UUID()
    var string : String
}

enter image description here

2
  • What happens if you just leave the UIView at intrinsic size by removing the sizeToFit when setting it up and just have SwiftUI resize by setting the frame? Then add .fixedSize to the SwiftUI View to ignore offered size. Commented Feb 26, 2021 at 13:45
  • @CenkBilgen Tried the option you have suggested. It's not working. Commented Feb 28, 2021 at 4:51

1 Answer 1

2

ScrollView + LazyVStack instead of List solved the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.