title: How to make a UIViewRepresentable @ViewBuilder work with dynamic content?
body: <p>Is it possible to make a <code>UIViewRepresentable</code> view which takes a <code>ViewBuilder</code> argument work with dynamic content such as <code>ForEach</code> loops?</p>
  
  <p>I have the following <code>UIViewRepresentable</code> view which I’m using to drop down to <code>UIKit</code> and get some custom <code>UIScrollView</code> behaviour:</p>
  
  <pre class="lang-swift prettyprint-override"><code>struct CustomScrollView&lt;Content:View&gt;: UIViewRepresentable {
  
      private let content: UIView
      private let scrollView = CustomUIScrollView()
  
      init(@ViewBuilder content: () -&gt; Content) {
          self.content = UIHostingController(rootView: content()).view
          self.content.backgroundColor = .clear
      }
  
      func makeUIView(context: Context) -&gt; UIView {
          scrollView.addSubview(content)
          // ...
          return scrollView
      }
  
      func updateUIView(_ uiView: UIView, context: Context) {}
  
  }
  </code></pre>
  
  <p>This works fine with static content as follows:</p>
  
  <pre class="lang-swift prettyprint-override"><code>var body: some View {
      CustomScrollView {
          VStack {
              ForEach(1..&lt;50) { number in
                  Text(String(number))
              }
          }
      }
  }
  </code></pre>
  
  <p>But it fails with dynamic content, showing a blank view:</p>
  
  <pre class="lang-swift prettyprint-override"><code>var body: some View {
      CustomScrollView {
          VStack {
              ForEach(self.numbers) { number in
                  Text(String(number))
              }
          }
      }
  }
  </code></pre>
  
  <p>I understand that this is because when <code>makeUIView()</code> is called my dynamic data is empty, and it is later filled or updated. I evaluate my <code>UIViewRepresentable</code>’s <code>content</code> at init, and don’t update it in <code>updateUIView()</code>.</p>
  
  <hr>
  
  <p>How do you go about updating dynamic child content in <code>updateUIView()</code>? I tried capturing the <code>@ViewBuilder</code> parameter as an <code>@escaping</code> closure and evaluating it every time <code>updateUIView()</code> is called, which seems like the right solution (albeit inefficient?), but no luck so far.</p>