Non-Scrolling UICollectionView inside UITableViewCell Dynamic Height
You need to bind your collectionView delegate/datasource with TableViewCell and need to use below func in tableViewcell. Make sure to turn off the CollectionView Scrolling.
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
self.layoutIfNeeded()
let contentSize = self.cvSubcategory.collectionViewLayout.collectionViewContentSize
if self.cvSubcategory.numberOfItems(inSection: 0) < 4 {
return CGSize(width: contentSize.width, height: 120) // Static height if colview is not fitted properly.
}
return CGSize(width: contentSize.width, height: contentSize.height + 20) // 20 is the margin of the collectinview with top and bottom
}
Your project sample solution : https://github.com/thedahiyaboy/DynamicCollectionApp
For future purpose you can take ref from this post : Dynamic CollectionViewCell In TableViewCell Swift
Dynamic UICollectionView inside dynamic UITableViewCell
Create a subclass for collectionView and override intrinsicContentSize.
class DynamicCollectionView: UICollectionView {
override func layoutSubviews() {
super.layoutSubviews()
if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
return collectionViewLayout.collectionViewContentSize
}
}
In Interface builder change the class of your collectionView to DynamicCollectionView (subclass UICollectionView).
Set estimated cell size of UICollectionViewFlowLayout.
flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
Related Topics
Swift: Gradient Along a Bezier Path (Using Calayers)
Pull Notification Locally on Jailbroken Device
Healthkit Authorisation Status Is Always 1
Removing Wkwebview Accesory Bar in Swift
iOS Color to Transparent in Uiimage
How to Use Multiple Segues with One Uitableviewdelegate
JSON Parsing Using Nsjsonserialization in iOS
How to Write Output of Augraph to a File
Uploads Using Backgroundsessionconfiguration and Nsurlsessionuploadtask Cause App to Crash
Swift Error: Editor Placeholder in Source File
iOS Simulator Is Not Launching
Swift String Escaping When Serializing to JSON Using Codable
Usage of String.Range in Swift 3.0
How to Get Vcf Data with Contact Images Using Cncontactvcardserialization Datawithcontacts: Method
Could an iOS Application Read/Access Sms Text
Get Each Line of Text in a Uilabel
How to Create the Current Date (Or Any Date) as an Nsdate Without Hours, Minutes and Seconds