MoPub集合视图单元格设置背景色

时间:2018-12-07 19:57:03

标签: ios swift uicollectionview uicollectionviewcell mopub

对于我的应用程序,我有一个集合视图,该视图基于ViewController中的变量设置单元格的背景色。

MoPub将广告放置到CollectionView中时,如何设置广告背景以匹配其他单元格?

class RestController: UIViewController {
    @IBOutlet weak var collectionView: UICollectionView!

    var category: Category!

    // Ads
    var placer = MPCollectionViewAdPlacer()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        collectionView.mp_reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // AD Cell
        collectionView.register(AdCell.self, forCellWithReuseIdentifier: "cell")

        let settings = MPStaticNativeAdRendererSettings()
        settings.renderingViewClass = AdCell.self
        settings.viewSizeHandler = { (maxWidth: CGFloat) -> CGSize in
            return CGSize(width: 160, height: 100)
        }

        let config = MPStaticNativeAdRenderer.rendererConfiguration(with: settings)
        placer = MPCollectionViewAdPlacer(collectionView: collectionView, viewController: self, rendererConfigurations: [config as Any])

        let targeting = MPNativeAdRequestTargeting()
        targeting.desiredAssets = [ kAdStarRatingKey, kAdIconImageKey, kAdTitleKey, kAdPrivacyIconUIImageKey ]
        placer.loadAds(forAdUnitID: unitId, targeting: targeting)

        collectionView.mp_setDelegate(self)
        collectionView.mp_setDataSource(self)
    }
}

// MARK: UICollectionViewDataSource
extension RestController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return results.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.mp_dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! RestCell
        cell.backgroundColor = category.colors[1]
        return cell
    }
}

是否可以通过任何方式将此信息传递给Ad Cell或MoPub提供的用于设置该单元设计的任何方法?

0 个答案:

没有答案