我已将 admob 横幅添加到我的应用程序中,该横幅工作正常,除非表格 View 中的行没有完全填满屏幕。然后横幅就位于最后一行下方,我想强制它们“粘”在屏幕底部。非常感谢。
//MARK: Properties
// Ad banner and interstitial views
var adMobBannerView = GADBannerView()
let ADMOB_BANNER_UNIT_ID = "ca-app-pub-xxxxxxx"
override func viewDidLoad() {
super.viewDidLoad()
// Init AdMob banner
initAdMobBanner()
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return adMobBannerView
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return adMobBannerView.frame.height
}
// MARK: - ADMOB BANNER
func initAdMobBanner() {
if UIDevice.current.userInterfaceIdiom == .phone {
// iPhone
adMobBannerView.adSize = GADAdSizeFromCGSize(CGSize(width: 320, height: 50))
adMobBannerView.frame = CGRect(x: 0, y: view.frame.size.height, width: 320, height: 50)
} else {
// iPad
adMobBannerView.adSize = GADAdSizeFromCGSize(CGSize(width: 468, height: 60))
adMobBannerView.frame = CGRect(x: 0, y: view.frame.size.height, width: 468, height: 60)
}
adMobBannerView.adUnitID = ADMOB_BANNER_UNIT_ID
adMobBannerView.rootViewController = self
adMobBannerView.delegate = self
view.addSubview(adMobBannerView)
let request = GADRequest()
adMobBannerView.load(request)
}
// Hide the banner
func hideBanner(_ banner: UIView) {
banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height - banner.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
banner.isHidden = true
}
// Show the banner
func showBanner(_ banner: UIView) {
banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height - banner.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
banner.isHidden = false
}
// AdMob banner available
func adViewDidReceiveAd(_ view: GADBannerView) {
// Reposition the banner ad to create a slide up effect
let translateTransform = CGAffineTransform(translationX: 0, y: adMobBannerView.bounds.size.height)
adMobBannerView.transform = translateTransform
showBanner(adMobBannerView)
UIView.animate(withDuration: 0.5) {
self.adMobBannerView.transform = CGAffineTransform.identity
}
}
// NO AdMob banner available
func adView(_ view: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
// Reposition the banner ad to create a slide up effect
let translateTransform = CGAffineTransform(translationX: 0, y: adMobBannerView.bounds.size.height)
adMobBannerView.transform = translateTransform
hideBanner(adMobBannerView)
UIView.animate(withDuration: 0.5) {
self.adMobBannerView.transform = CGAffineTransform.identity
}
}
我使用过这两个教程:
https://www.appcoda.com/google-admob-ios-swift/
https://code.tutsplus.com/tutorials/how-to-add-admob-banner-ads-to-your-ios-swift-app--cms-27403
最佳答案
我花了很长时间尝试人们的建议并继续谷歌搜索。这个问题已经困扰我两周了,我很高兴地说我已经用一行代码解决了它。
当我将 tableview 嵌入到导航 Controller 中时(如果您不知道如何操作,请参阅此处:Apple tute table navigation ),我看到人们提到将 subview 添加到 UITableViewController 的“ontop”导航 Controller 。
所以我只是改变了这一行:
view.addSubview(adMobBannerView)
到
self.navigationController?.view.addSubview(adMobBannerView)
我还删除了以下内容,因为横幅广告不再是 uitableviewcontroller 的一部分:
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return adMobBannerView
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return adMobBannerView.frame.height
}
我的代码的其余部分仍然用于横幅放置、动画等,并且它们仍然被应用并且工作完美。
我希望这可以帮助其他有同样问题的人。
哦,如果你需要删除这个 subview ,它也是一个单行 -
admobBannerView.removeFromSuperview()
关于swift - Admob 横幅不粘在 uitableview swift 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47952575/