[英]iOS -How to Add GADBannerView to UICollectionReusableView
我將我的bannerView添加到collectionView標頭中。 它不會讓我將bannerView.rootViewController
設置為bannerView.rootViewController
自身,因為它不是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
}
}
在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
}
}
請遵循此答案以獲取有關bannerView如何知道何時顯示在屏幕上的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.