Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Created December 30, 2021 17:25
Show Gist options
  • Save StanislavK/da1d4379311ebc665efcc23afa3feff8 to your computer and use it in GitHub Desktop.
Save StanislavK/da1d4379311ebc665efcc23afa3feff8 to your computer and use it in GitHub Desktop.
struct GenericUIViewRepresentable<ViewType>: UIViewRepresentable where ViewType: UIView {
let factory: (Context) -> ViewType
func makeUIView(context: Context) -> ViewType {
factory(context)
}
func updateUIView(_ uiView: ViewType, context: Context) {}
}
@StanislavK
Copy link
Author

Usage:

struct ProfileView_Preview: PreviewProvider {
    static var previews: some View {
        let profile = Profile(
            name: "John Smith",
            url: URL(string: "https://www.google.com")!
        )
     return GenericUIViewRepresentable { _ in
            return ProfileView(profile: profile)
      }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment