The World’s Largest Online Community for Developers

'; ios - Uicollection view dynamic height but fixed width - LavOzs.Com

I am working with UICollectionView I have 4 different kind of cells. Every cell is designed in xib file . when i load them in collection view it goes out of screen. i don't know why it is happening. Some cells have fixed height while some have dynamic height (depends on data coming from API). So is there any possible way to solve this issue. i have tried Estimate size automatic to dynamic

override func awakeFromNib() {
    self.contentView.translatesAutoresizingMaskIntoConstraints = false
    let screenwidth = UIScreen.main.bounds.size.width
    widthAnchor.constraint(equalToConstant: screenwidth-20)

func setSize () {
    let layout = mainCollection.collectionViewLayout as! UICollectionViewFlowLayout
    layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)}

}screenshot of how end portion of cell is going out of screen

try this!

let screenWidth = self.CollecionView.frame.width
Passing Data between View Controllers
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
UICollectionView edge to edge layout
Pin cell height to UICollectionView height
Dynamic-height UICollectionView inside a dynamic-height UITableViewCell
UICollectionView full width and height problem