title: Self resizing for horizontal UICollectionView single row layout - not cell
body: <p><strong>Use Case:</strong></p>
  
  <ul>
  <li>I have a <code>UICollectionView</code> to display products, let us call it <code>productsViewCollection</code> (only one row with multiple <code>ProductCell</code>s).</li>
  <li>Each <code>ProductCell</code> may has different height, but all of them have the same width.</li>
  <li><code>ProductCell</code> may be added at the run time.</li>
  <li>I am using self resizing cells depending on the <code>AutoLayout</code> for the <code>ProductCell</code>s.</li>
  <li><p>I have <code>UICollectionViewFlowLayout</code> custom class to make the product cells alignment top.</p>
  
  <pre><code>class TopLayout : UICollectionViewFlowLayout {
  
  override func awakeFromNib() {
      super.awakeFromNib();
      self.scrollDirection = .horizontal;
  }
  
  override func layoutAttributesForElements(in rect: CGRect) -&gt; [UICollectionViewLayoutAttributes]? {
      let attributesArray = super.layoutAttributesForElements(in: rect);
  
      if attributesArray != nil {
          for attributes in attributesArray! {
              if (attributes.representedElementKind == nil) {
                  let rect : CGRect? = self.layoutAttributesForItem(at: attributes.indexPath)?.frame;
                  if (rect != nil) {
                      attributes.frame = rect!;
                  }
              }
          }
      }
  
      return attributesArray;
  }
  
  override func layoutAttributesForItem(at indexPath: IndexPath) -&gt; UICollectionViewLayoutAttributes? {
      let attributes = super.layoutAttributesForItem(at: indexPath);
      //let sectionInset : UIEdgeInsets = (self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout).sectionInset;
  
      if (indexPath.item &gt;= 0) {
          attributes?.frame.origin.y = 0;
      }
      return attributes;
  }}
  </code></pre></li>
  </ul>
  
  <p><strong>What is the problem?</strong></p>
  
  <ul>
  <li>(1) I didn't find a way to determine the height for the <code>productsCollectionView</code>.</li>
  <li>(2) I didn't find a way to force the <code>productsCollectionView</code> to display only one row.</li>
  <li>(3) Inserting a new <code>ProductCell</code> into the <code>productsCollectionView</code> leading to scroll to the first item!, I need to keep it at the same position.</li>
  </ul>
  
  <p><strong>What I tried?</strong></p>
  
  <ul>
  <li>Tries for issue (1):
  
  <ul>
  <li>Determining the width without the height will not work, because I need to determine the height for the <code>productsCollectionView</code> otherwise the <code>ProductCell</code>s will not appear.</li>
  <li>Therefore I tried to set height constraint but the app has crashed <code>"The behavior of the UICollectionViewFlowLayout is not defined because the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values."</code>, <code>productsCollectionView</code> don't have insets (<a href="https://stackoverflow.com/questions/34300895/horizontal-uicollectionview-single-row-layout">Horizontal UICollectionView single row layout</a>).</li>
  <li>I tried also to use <code>collectionView.contentSize</code> with height constraint to determine the height for <code>productsCollectionView</code> but it didn't work because it will not have the final calculated size even if you called <code>layoutIfNeeded</code> or any similar method (<a href="https://stackoverflow.com/questions/13788522/how-to-determine-height-of-uicollectionview-with-flowlayout">How to determine height of UICollectionView with FlowLayout</a> and many other links).</li>
  <li>I tried also to use <code>collectionView.collectionViewlayout.collectionViewContentSize</code> with height constraint to determine the height for <code>productsCollectionView</code> but it didn't work, it will calculate the width correctly while the height will be determined according to the <code>AutoLayout</code> constraints not depending on the maximum <code>ProductCell</code> height, so I didn't find a way to determine the height automatically (<a href="https://stackoverflow.com/questions/13788522/how-to-determine-height-of-uicollectionview-with-flowlayout">How to determine height of UICollectionView with FlowLayout</a> and many other links).</li>
  <li>Therefore, finally I tried to change the height depending on the <code>ProductCell</code>s max height manually, I set larger height constraint for the <code>productsViewCollection</code> and in the <code>MyViewController</code> I called <code>layoutAttributesForItem</code> for each item in the <code>productsViewCollection</code> to calculate each <code>ProductCell</code> height.</li>
  <li>It worked, but I should do the same thing if I need to insert any other products, but also this leaded to wrong behavior in the UI, the <code>productsCollectionView</code> will expand for some of milliseconds then crop correctly :(.</li>
  </ul></li>
  <li>Tries for issue (2):
  
  <ul>
  <li>Of course I made it horizontal but this is not enough, the flow layout will add products to the same column in case the sum of products heights is less than the <code>productsCollectionView</code> height (<a href="https://stackoverflow.com/questions/31046631/display-one-row-in-uicollectionview-swift">Display one row in UICollectionView - Swift</a>).</li>
  <li>So I tried to set larger value for <code>minimumInteritemSpacing</code> it worked but not 100% correctly, it will add extra space at the end (<a href="https://stackoverflow.com/questions/13918276/uicollectionview-one-row-or-column">UICollectionView: One Row or Column</a>).</li>
  <li>I tried to make the <code>ProductCell</code> height same as the <code>productsCollectionView</code> height using custom <code>UICollectionViewFlowLayout</code> class in the <code>layoutAttributesForItem</code> method, but it didn't work, heights changed but the cells at the same column, I don't know why!</li>
  </ul></li>
  <li>Tries for issue (3):
  
  <ul>
  <li>I don't know why inserting new item leaded to scrolling, sometimes for the first item, sometimes before few items, sometimes it works correctly with strange resizing animation!. Anyway, I tried to disable my resizing <code>productsCollectionView</code> code and nothing happened.</li>
  <li>I tried to save current <code>productsCollectionView.contentOffset</code> then apply it, and it worked only if I set the <code>animated</code> parameter equals to <code>true</code>, otherwise it will be unspecified scrolling.</li>
  </ul></li>
  </ul>
  
  <p><strong>Note:</strong> 
  Sorry for my bad English &amp; Thanks.</p>