How to add a static ad banner in a UITableViewController?

I want to add AdMob banners in my application. I put them at the bottom of each screen and it works correctly, but not completely, when it comes to UIViewControllers.

When I add an add to the UITableViewController, it starts in the right place at the bottom of the screen, but when I look at the table, it moves with it. I need the ad to statically stay at the bottom of the screen when I view the table.

Here is my code:

- (void)displayGAD { // The frame of the banner is initialized off screen. // If an ad loads then it will animate onto the screen. self.bannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)]; self.bannerView.adUnitID = self.adUnitID; self.bannerView.delegate = self; self.bannerView.rootViewController = self; [self.view addSubview:self.bannerView]; [self.bannerView loadRequest:[self createRequest]]; } - (GADRequest *)createRequest { GADRequest *request = [GADRequest request]; #warning Comment this out before distribution request.testDevices = [NSArray arrayWithObjects:@"84ea3d9789cabb0a34176cbb52c0f992", @"abf08fe141b95987d27ac068602605b8", GAD_SIMULATOR_ID, nil]; return request; } - (void)adViewDidReceiveAd:(GADBannerView *)view { NSLog(@"Received Ad"); [UIView animateWithDuration:1.0 animations:^ { view.frame = CGRectMake(0.0, self.view.frame.size.height - view.frame.size.height, view.frame.size.width, view.frame.size.height); }]; } - (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error { NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]); } 

I saw several examples of how I can insert an ad into a table view, but not something specific for how I want to do this. The only thing I read regarding this is that I have to put the table view in the container view and then add an ad to it. I do not know how I would do this, since it is a UITableViewController.

+4
source share
1 answer

I realized that the easiest way to achieve this is to not use a dedicated UITableViewController. I created a UIViewController and added a container controller to the view. From there, I subclassed the container controller as a UITableViewController. I just put all my code related to the table view in this subclass. Then I placed the loading and placement of my ad at the top level of the UIViewController. Running this method means that the ad is embedded in the same view as this container controller. I just made it so that my ad banner was on top of the container. This leads to the fact that I have the ability to scroll with the appearance of the table, and the banner does not move.

+3
source

Source: https://habr.com/ru/post/1501552/


All Articles