【问题标题】:iOS -How to Add GADBannerView to UICollectionReusableViewiOS - 如何将 GADBannerView 添加到 UICollectionReusableView
【发布时间】:2019-12-15 16:29:38
【问题描述】:

我在 collectionView 标头中添加了我的bannerView。它不会让我将 bannerView.rootViewController 设置为 self 到 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())
    }
}

包含 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
    }
}

【问题讨论】:

    标签: ios swift admob adbannerview uicollectionreusableview


    【解决方案1】:

    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 并投放广告的类位于主 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
        }
    }
    

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

    【讨论】:

    • 尝试将视图直接添加为标题中的子视图。我认为不需要 PassThroughView
    • 直通视图有什么问题?它工作正常,我一直在上下滚动,没有问题
    • PassThroughView 如果您想将触摸事件传递给其下方的视图/控件,则需要。而如果您直接将其添加为子视图,就像您在答案中所做的那样,而是直接添加。那么就不需要passthroughview了。
    • 我明白了。在我的实际项目中,我对广告进行了圆角处理,因此它与所有其他具有圆角的对象看起来更加原生。只添加一个视图然后尝试绕过标题本身的角落更容易。感谢您的提示,非常感谢!干杯!!! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2021-09-20
    • 2017-04-28
    相关资源
    最近更新 更多