简体   繁体   中英

Advertisement is covering tableview

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.

表格视图的图像

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"
}

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
}

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM