Reputation: 445
I've added admob banners into my app which are working fine except when my rows in the tableview don't completely fill up the screen. The banners then just sit under the last row, I'd like to force them to stay "stuck" to the bottom of the screen. Thanks very much.
//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
}
}
I've used these 2 tutorials:
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
Upvotes: 3
Views: 1983
Reputation: 445
I have spent ages trying what people have suggested and continued googling. This problem has been bugging me for 2 weeks and I'm glad to say I have solved it with one line.
As I embedded the tableview within a navigation controller (see here if you don't know how to do that: Apple tute table navigation ) I had seen people mention adding subviews to the navigation controller "ontop" of the UITableViewController.
So I simply changed this one line:
view.addSubview(adMobBannerView)
to
self.navigationController?.view.addSubview(adMobBannerView)
I also removed the following as the banner ad was no longer part of the 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
}
The rest of my code is still used for banner placement, animation etc and it's all still applied and working flawlessly.
I hope this helps others who have the same problem.
Oh, and if you need to remove this subview it's also a one liner -
admobBannerView.removeFromSuperview()
Upvotes: 3
Reputation: 507
You can Do it Like below Image. add bannerview in Bottom of View. you added banner in footerview of tableView.
Upvotes: 0
Reputation: 1136
As you already know, iPhone and iPad comes in various dimensions in both width and height of the screen.
In code, you are providing constant height, width and placement of ADMOB frames and will work well in iPhone for 5,5s and SE version,
for rest of upper screens models, You have to set them dynamically,
To do the same here is my solution:
I think you have to update the method to initiate the frame and place of the ad mob
if UIDevice.current.userInterfaceIdiom == .phone {
// iPhone
adMobBannerView.adSize = GADAdSizeFromCGSize(CGSize(width: view.frame.size.width, height: 50))
adMobBannerView.frame = CGRect(x: 0, y: view.frame.size.height - 50.0, width: view.frame.size.width, height: 50)
} else {
// iPad
adMobBannerView.adSize = GADAdSizeFromCGSize(CGSize(width: view.frame.size.width, height: 60))
adMobBannerView.frame = CGRect(x: 0, y: view.frame.size.height - 60.0, width: view.frame.size.width, height: 60)
}
In this, I am first getting the device height and width from view itself.
Upvotes: 1