【发布时间】: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