title: How can I dynamically add SwiftUI Views to a parent View?
body: <p>Is it possible to add multiple SwiftUI Views to a parent View dynamically &amp; programmatically?</p>
  
  <p>For example suppose we have a basic View such as:</p>
  
  <pre><code>struct MyRectView: View {
      var body: some View {
          Rectangle()
              .fill(Color.red)
              .frame(width: 200, height: 200)
      }
  }
  </code></pre>
  
  <p>And a Button defined as:</p>
  
  <pre><code>struct MyButtonThatMakesRects: View {
  
      var body: some View {
          Button(
              action: {
                  // Create &amp; add Views to parent here?
                  // ...
              },
              label: {
                  Text("tap to create a Rect")
              }
          )
      }
  }
  </code></pre>
  
  <p>Is there any way I can create multiple instances of <code>MyRectView</code> in a parent View when <code>MyButtonThatMakesRects</code> is tapped?</p>
  
  <p>My initial thinking was in line with how I would do this in UIKit. That being on button tap, create a new <code>UIView()</code>, and then use <code>.addSubview(...)</code> to add it to a parent. Not sure if SwiftUI has similar functionality. Or maybe there is a simpler way to do this that I'm not seeing?</p>