gpt4 book ai didi

iOS - 如何将 GADBannerView 添加到 UICollectionReusableView

转载 作者:行者123 更新时间:2023-11-29 05:32:33 27 4
gpt4 key购买 nike

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

enter image description here

我总是可以在具有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
}
}

最佳答案

headerView内我添加了一个PassthroughViewcellForItem内部,我将bannerView作为 subview 添加到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 如何知道何时出现在屏幕上以及何时不在屏幕上的更多信息。

关于iOS - 如何将 GADBannerView 添加到 UICollectionReusableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57404504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com