Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languageswift
class InfScrlViewCell: UITableViewCell {

    let centeredLabellabel = UILabel()
    // This is the wrapper
    let adContainer:UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        // Specify the item ad tag here
        view.accessibilityLabel = "infiniteScroll_ad_wrapper_tag"
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(centeredLabellabel)
        // Added the wrapper to the item
        contentView.addSubview(adContainer)
        // Setup the constraints
        ...
    }
    ...
}

...

Code Block
languageswift
import UIKit
import R89SDK

class CollectionViewInfiniteScrollViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
 
    var collectionView: UICollectionView!
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        // Configure the collection view layout
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        // Configure the collection view
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(DynamicHeightCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        // Add constraints to fill the view
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(collectionView)
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        ])
    }
    ...
}

Adding the wrapper to the item

The adContainer will play the role of a wrapper inside the UICollectionView’s contentView.

Code Block
languageswift
class DynamicHeightCollectionViewCell:  UICollectionViewCell {
    
    private let label = UILabel()
    
    private let adContainer: UIView = {
        let view = UIView()
        // Specify the item ad tag here
        view.accessibilityLabel = "infiniteScroll_ad_wrapper_tag"
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(label)
        contentView.addSubview(adContainer)
    }
    
    // MARK: - Setup Methods
    private func setupViews() {
        contentView.addSubview(label)
        contentView.addSubview(adContainer)
    }
    
    // MARK: - Configuration
    func configure(title: String, subtitle: String) {
        titleLabel.text = title
        subtitleLabel.text = "Ad box heigth is \(adContainer.frame.height) \n Childcount is \(adContainer.subviews.count)"
    }
}