2

I've created a tableview which has an AdMob advertisement banner in the bottom of it. If the user has premium the ad will not be shown, using the code advertisementBanner.isHidden = true .

The problem is that when the advertisement is shown I cannot scroll "to the bottom" since it's already scrolled down all the way, but the ad is covering the area.

The image of the tableview

As you can see I'm not on the bottom, and I cannot scroll down more since I'm already at the "bottom". So my question now is:

Is there any way I can make my "scroll" even longer? Or do you have another alternative I can do to fix this problem? Thanks.

This is the code I've currently:

//If no advertisement is shown.
func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError)
{
    advertisementBanner.isHidden = true
}
func adViewDidReceiveAd(_ bannerView: GADBannerView)
{
    //What should I insert here to "make tableview even longer"
}
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
SwiftPrezy
  • 21
  • 4
  • 1
    Create an outlet of your TableViewHeightConstraint, and when you want to show the add, just reduce the HeightConstraint by your adView height. – manishsharma93 Jun 27 '19 at 09:36
  • @manishsharma93 How can I do this? I mean, outlet for TableViewHeightConstraint? – SwiftPrezy Jun 27 '19 at 09:39
  • Refer this https://stackoverflow.com/a/22110084/5084797 – manishsharma93 Jun 27 '19 at 09:51
  • @manishsharma93 Thanks a lot, I managed to create the outlet. Now for the next problem, when the ad is shown I'm adding this one: `TableViewHeightConstraint.constant = 200` But when I run the program it's not getting resized somehow. I've also added the `tableView.layoutIfNeeded() tableView.updateConstraints()` – SwiftPrezy Jun 27 '19 at 10:14
  • Are you using tableview in a view controller or using UITableViewController? – RajeshKumar R Jun 27 '19 at 10:29
  • @RajeshKumarR ViewController, I might have found a way to figure this out. Lemme check – SwiftPrezy Jun 27 '19 at 10:39
  • Add a view at the bottom (after tableview) and keep its heightConstraint to 0.Now whenever you receive a adView/BannerView in adViewDidReceiveAd Function, simply just set the height of AddedView to bannerView.height and call LayoutIfNeeded(), We have been using this technique in 50+ applications. – Friend Jun 27 '19 at 13:29

3 Answers3

0

You can move your adView to tableViews footerView.

func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError)
{
    tableView.tableFooterView = bannerView
}
func adViewDidReceiveAd(_ bannerView: GADBannerView)
{
    tableView.tableFooterView = nil
}
0

Thanks to @manishsharma93, I managed to fix my problem.

Instead of adding @IBOutlet to Height, I added it to the bottom constraint.

@IBOutlet weak var topConstraint: NSLayoutConstraint!

And from there I added a constraint to change the space between tableview and navigationcontroller, like this:

func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError)
{
    advertisementBanner.isHidden = true //Hiding the ad banner
    topConstraint.constant = 0 //Adding 0 constraint from bottom to tableview

    tableView.layoutIfNeeded()
    tableView.updateConstraints()
}

And then I have this if the ad successfully deployed:

func adViewDidReceiveAd(_ bannerView: GADBannerView)
{
    topConstraint.constant = 100 //Adding 100 "constraints" to bottom

    tableView.layoutIfNeeded()
    tableView.updateConstraints()
}

I've added "100" to it because the advertisement banner's height is 100.

SwiftPrezy
  • 21
  • 4
0

In your storyboard create a view of height 200 and adjust it at bottom of the screen. Set your Tableview to its above. Now load Admob Ad inside your bottom view. Then Create an outlet for its Height constraint to your swift file. like

@IBOutlet var adViewHeight: NSLayoutConstraint!

Now adjust your logic like below

if (showAd) // depending on your location
{
     adView.isHidden = false;
     adViewHeight.constant = 200;
}  
else { // hide ad
     adView.isHidden = true;
     adViewHeight.constant = 0;
}
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36