0

I have a subclass of UIControl which contains (and controls) a UICollectionView. I'm trying to get it to auto-resize to fit the collectionView, but haven't had any success yet.

The collection view is set up in a common init method of the UIControl subclass as follows:

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = 0
layout.itemSize = CGSize(width: 30, height: 30)

collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.isUserInteractionEnabled = false
collectionView.isScrollEnabled = false
collectionView.delegate = self
collectionView.dataSource = self
addSubview(collectionView)

If anyone could help with this, I'd greatly appreciate it! Thanks in advance.

JacobCXDev
  • 417
  • 6
  • 16
  • Did you try using layout constraints? – Sahil Kapoor Apr 24 '20 at 03:06
  • @SahilKapoor I have tried, but as of yet, I've been unsuccessful. I wasn't quite sure what I was doing wrong, so I removed them from the code above in the hopes that someone can point me in the right direction. – JacobCXDev Apr 24 '20 at 03:08

2 Answers2

0

you can create a new custom Method in UIControl subclass and move init code in this method.Than in collectionview willdisplay delegate method called that custom method.

Anil Kumar
  • 1,830
  • 15
  • 24
0

I found the answer here. Subclassing the UICollectionView like so fixed it.

JacobCXDev
  • 417
  • 6
  • 16