IlyaGutnikov IlyaGutnikov - 15 days ago 6
iOS Question

Autoresize UICollectionViewCell size inside UITableView

I am trying to create simple educational app about movies, and I need to create movie frames horizontal scroller.

I create collection view for that: enter image description here

But if I build it for another target (iPhone 6s Plus for example), I have this:
enter image description here

How to solve this problem?

My scene controller code (Swift 3):

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = UIColor.black

// Configure the cell
return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return UITableViewAutomaticDimension
}

Answer

I think that you can try to implement the method of UICollectionView delegate:

   func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(yourHeight, self.view.frame.size.width)
}

in this way, your collectionView-cell inside in tableView will appear as big as the screen.