Enkha
Enkha

Reputation: 430

Resizing the .xib (cell) vertically in Swift 4

I created a .xib (cell) that displays store images and store information.

enter image description here

Gray is the portion of the image that is not resized and is a UICollectionView.

All stores have different sizes, but in UICollectionView (which displays images) they are displayed as Scale To Fill regardless of size.

I would like the .xib vertical size (UICollectionView) to grow and shrink to fit the vertical size of the image. What should I do?

Upvotes: 1

Views: 90

Answers (1)

lionserdar
lionserdar

Reputation: 2032

If you are dealing with UICollectionViewCell you don't really need to change/update the xib height or size directly. More importantly what you need is CollectionViewFlowLayout methods.

I think what you need is

1. In your xib, you should properly layout your items with `Autolayout`. 
2. Make sure that you set the `collectionview.delegate = self` somewhere based on your need
3. Add `UICollectionViewDelegateFlowLayout` methods and return proper values.


 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: `width`, height: `height`)
}

The above delegate method should be enough but you may want to use others based on your need.

Upvotes: 1

Related Questions