iOS - 如何将 GADBannerView 添加到 UICollectionReusableView

iOS -How to Add GADBannerView to UICollectionReusableView

我正在将我的 bannerView 添加到 collectionView header 中。它不会让我将 bannerView.rootViewController 设置为 headerView 的自身,因为它不是 UIViewController。

我总是可以在具有 collectionView 的 viewController 中实现所需的属性,但是我该如何加载 bannerView?

class HeaderView: UICollectionReusableView {

    var bannerView: GADBannerView = {
        let view = GADBannerView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white

        bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

        bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        bannerView.rootViewController = self
        bannerView.load(GADRequest())
    }
}

class 包含 collectionView:

class MainViewController: UIViewController {

    var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // ... instantiate the collectionView
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath) as! HeaderView

        return headerView
    }
}

我在 headerView 中添加了一个 PassthroughView,在 cellForItem 中我将 bannerView 作为子视图添加到 PassthroughView。工作正常

import GoogleMobileAds

class HeaderView: UICollectionReusableView {

    var passthroughView: PassthroughView = {
        let view = PassthroughView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.isUserInteractionEnabled = true
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white

        // anchors for passthroughView ...
    }

    func addBannerViewToPassthroughView(_ bannerView: GADBannerView) {

        if !bannerView.isDescendant(of: passthroughView) {

            passthroughView.addSubview(bannerView)
        }
    }
}

包含 collectionView 并投放广告的 class 位于主要 vc 而不是单元格中,因此我不必担心单元格会自行回收并不断投放广告它已滚动:

class MainViewController: UIViewController {

    var collectionView: UICollectionView!

    var bannerView: GADBannerView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // ... instantiate the collectionView

        bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
        bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        bannerView.rootViewController = self
        bannerView.delegate = self
        bannerView.load(GADRequest())
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath) as! HeaderView

        // for some strange reason adding the bannerView as a subView to the passthroughView inconsistently froze my app (sometimes it did and sometimes it didn't). Once I added it on the mainQueue everything worked fine
        DispatchQueue.main.async { [weak self] in
            if let bannerView = self?.bannerView {
                headerView.addBannerViewToPassthroughView(bannerView)
            }
        }

        return headerView
    }
}

关注 以了解有关 bannerView 如何知道它何时出现和不出现在屏幕上的更多信息。