【问题标题】:Handling Events On Admob UICollectionView处理 Admob UICollectionView 上的事件
【发布时间】:2021-12-30 22:48:41
【问题描述】:

对 Admob 来说相当陌生。我一直在尝试在 UICollectionView 上实施 AdMob 原生广告,但我几乎没有运气。我已经能够在单元格中加载和显示广告,但未注册事件,例如使用 GADNativeAdDelegate 进行查看或点击。这是到目前为止的代码:

// In main view controller
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "exampleCell", for: indexPath) as! exampleCell
 let myItem = myItemList[indexPath.item]
         if myItem is GADNativeAd {
        let nAd = myItems as! GADNativeAd
        let nibView = Bundle.main.loadNibNamed("UnifiedNativeAdView", owner: nil, options: nil)?.first
        let nativeAdView = nibView as! GADNativeAdView
        
        // Prepare ad content
        let nativeAd = nAd
        
        // Add content to native view
        (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
          nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
        
        (nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
        nativeAdView.bodyView?.isHidden = nativeAd.body == nil
        
        (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
          nativeAdView.iconView?.isHidden = nativeAd.icon == nil
        
        (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
          nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil
        
        nativeAdView.callToActionView?.isUserInteractionEnabled = false
        nativeAdView.nativeAd?.delegate = self
        nativeAdView.nativeAd?.rootViewController = self
        cell.addSubview(nativeAdView)

        return cell
     }
}

我的单元格是一个基本的 UICollectionViewCell,如下所示:

class exampleCell: UICollectionViewCell {

     override func awakeFromNib() {
        super.awakeFromNib()
    }

} 

我看过this answer,但它似乎不适用。

【问题讨论】:

    标签: ios swift xcode uicollectionview admob


    【解决方案1】:

    在 cellForItemAt 委托中 试试这个 // 在添加新的之前从内容视图中删除以前的 GADBannerView。

    for subview in reusableAdCell.contentView.subviews {
          
    subview.removeFromSuperview()
      
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      相关资源
      最近更新 更多