title: Is there a way to update views in StackView without removing the view and re adding it?
body: <p>I have views which use autolayout. I know the exact width I want each view to be (each width is different). The StackView is nested inside of a collection view. The views inside the stack view are loaded dynamically from the main view controller. I want to update the stack views views when the collection view reloads without removing the views and adding them again. I cannot remove the views and re add them because some of the views have textfields which will dismiss the keyboard if reloaded. I have explained the code below</p>
  <p>Thank you for any help.</p>
  <hr />
  <h1>EDIT</h1>
  <p>This is the layout I am looking for.</p>
  <p><a href="https://i.stack.imgur.com/zmp6K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zmp6K.png" alt="example" /></a></p>
  <p>I use <a href="https://github.com/ra1028/Carbon" rel="nofollow noreferrer">Carbon</a> for rendering the views into the collection view:</p>
  <pre class="lang-swift prettyprint-override"><code>    private func render() {
          renderer.render {
              Section(
                  id: &quot;testing row collection&quot;,
                  cells: {
                      Row(id: &quot;row id&quot;, data: [
                          Input(
                              id: 6,
                              props: InputContent.State(
                                  text: self.state.inputText,
                                  placeholder: &quot;Search&quot;,
                                  size: .medium
                              ),
                              onChange: { [weak self] text in
                                  print(&quot;text is:&quot;, text)
                                  self?.state.inputText = text
                              }
                          )
                      ])
                  }
              )
          }
      }
  </code></pre>
  <p>NOTE: the image and code do not match implementation. The image shows 3 views, the code only shows 1.</p>
  <p>The class <code>Row</code> is the class that renders the view that holds the UIStackView. Inside that I have a list of views, at the moment only 1 view. It's sets <code>text</code> to be <code>self.state.inputText</code> which is a variable in the view controller. Then on any change in the textfield I set <code>self.state.inputText</code> to be that text. When the variable <code>state</code> is set, <code>render()</code> is run.</p>
  <p>The <code>Row</code> class:</p>
  <p>This class renders the actual UIView which is called <code>RowContent</code>. It calls the <code>updateState(state:)</code> method shown in the <code>RowContent</code> class. This is called when the collection view renders</p>
  <p>The <code>RowContent</code> class:</p>
  <pre class="lang-swift prettyprint-override"><code>class RowContent: UIView {
  
      var stack: UIStackView
  
      var state: State
      
      struct State {
          var views: [UIView] = [UIView]()
      }
      
      public func updateState(_ state: State? = nil) {
          self.state = state ?? self.state
          //updateView()
      }
      
      required init(state: State) {
          self.state = state
          self.stack = UIStackView(arrangedSubviews: state.views)
          super.init(frame: .zero)
          self.setupStackView()
      }
  
      ...
  
  }
  </code></pre>
  <p>The stack view gets populated in the initialiser with the views, then I add the constraints in <code>setupStackview()</code></p>
  <p>At the moment, the views never update when the collection view re renders, meaning that the text property in Input never gets updated. I want to update the views when the collection view re renders. This would be in the <code>updateView()</code> method which is commented out. I do not know how to update the views in the collectionView without removing the views and re adding them.</p>
  <p>Thanks.</p>
  <p>I have added an example here: <a href="https://github.com/4ndrewHarri5/ExampleRow" rel="nofollow noreferrer">https://github.com/4ndrewHarri5/ExampleRow</a></p>