title: CollectionView dynamic height with Swift 3 in iOS
body: <p>Recently, I'm trying to do some projects for practicing. So I watched the course "Developing Apps for iOS", Stanford University, CS193P, 2017.</p>
  
  <p>And now, I'm doing "Smashtag" project but I have some problems in it.
  I want to use a CollectionView with two UICollectionViewFlowLayout to show two types in each xib (one is like tableView, another is showing a image in square) of CollectionView by SegmentedControl. </p>
  
  <p>My problems below : </p>
  
  <blockquote>
    <ol>
    <li><p>How to make CollectionViewCell showing in dynamic height just like TableView's UITableViewAutomaticDimension?</p>
    
    <p>And this is what I just tried below : </p>
    
    <p>I tried to make CollectionView's autoResize setting as like TableView's autoDimension with using <strong><em>UICollectionViewFlowLayoutAutomaticSize. ( @available(iOS 10.0, *) )</em></strong></p></li>
    </ol>
  </blockquote>
  
  <pre><code>enum CollectionViewType {
    case tweet
    case image
  }
  
  // MARK: - Decide What Kind Of FlowLayout
  func decideFlowLayout(type: CollectionViewType) -&gt; UICollectionViewFlowLayout {
  
      // just for .image type
      var howManyImageShowing = 3  
      var imageShowingWidth: Double {
          return Double(self.view.frame.width) / Double(howManyImageShowing)
      }
  
      // .tweet is a type showing like TableViewCell -&gt; this make me confused !!
      // another type is just showing a image in square -&gt; I have no problem here
      let estimatedItemSize = type == .tweet ? CGSize(width: self.view.frame.width, height: 155.0) :
                                                  CGSize(width: imageShowingWidth, height: imageShowingWidth)
  
      let collectionViewLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
      collectionViewLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  
      // --------- Make this setting as TableView does --------- 
      // ------------ Somethis just like this below ------------
      /*
          tableView.estimatedRowHeight = 155.0
          tableView.rowHeight = UITableViewAutomaticDimension
      */
  
      collectionViewLayout.estimatedItemSize = estimatedItemSize
      collectionViewLayout.itemSize = UICollectionViewFlowLayoutAutomaticSize
      // ------------------------------------------------------- 
  
      collectionViewLayout.minimumLineSpacing = 0
      collectionViewLayout.minimumInteritemSpacing = 0
  
      return collectionViewLayout
  }
  </code></pre>
  
  <p>Constraint of my CollectionViewCell xib here :
  <a href="https://i.stack.imgur.com/X1DcT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/X1DcT.png" alt="enter image description here"></a>
  But it has bug when I run this app</p>
  
  <p>I don't know what I miss or misunderstand.
  <a href="https://i.stack.imgur.com/nSHSW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/nSHSW.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <ol start="2">
    <li><p>Why this solution will work ?</p>
    
    <p>I already search so many solutions of this problem in Stack Overflow but I just find this and make me really confused :(</p>
    
    <p>This solution give the view a <strong><em>Width Constraint</em></strong> which is in CollectionViewCell xib and set this constraint's constant a value and it works! I can not configure out why <strong><em>Width Constraint</em></strong> will make the cell's height in dynamic? This make me really confused, I think this is weird...</p>
    
    <p><a href="https://github.com/tttsunny/CollectionViewAutoSizingTest" rel="noreferrer">https://github.com/tttsunny/CollectionViewAutoSizingTest</a></p></li>
    </ol>
  </blockquote>
  
  <pre><code>class Cell: UICollectionViewCell {
      @IBOutlet weak var headerLabel: UILabel!
      @IBOutlet weak var descriptionLabel: UILabel!
      @IBOutlet weak var widthConstraint: NSLayoutConstraint!
  
      override func awakeFromNib() {
          super.awakeFromNib()
          // Initialization code
          self.contentView.translatesAutoresizingMaskIntoConstraints = false
          let screenWidth = UIScreen.main.bounds.size.width
          widthConstraint.constant = screenWidth - (2 * 12)
      }
  }
  </code></pre>
  
  <blockquote>
    <ol start="3">
    <li><p>How to do CollectionViewCell dynamic height without using <strong><em>UICollectionViewFlowLayoutAutomaticSize. ( @available(iOS 10.0, *) )</em></strong> ? </p>
    
    <p>Because I want to handle different iOS Versions not only in iOS 10 but also below iOS 10.</p></li>
    </ol>
  </blockquote>
  
  <p>I really want to be pro in iOS development and a great developer but I'm still learning and trying. Any replies I will be greatly appreciated. </p>
  
  <p>Thank you !!</p>