Question

I am using Admob as an Ad provider. I have a UIViewController that has a UITableView which is set in the Storyboard to use the whole width and height of the controller.

When an Ad is received the following occurs:

- (void)adViewDidReceiveAd:(GADBannerView *)Adview {

    if (bannerView_.hidden) {
        bannerView_.hidden = NO;
    }

    [UIView animateWithDuration:1.0 animations:^ {
        // Final frame of ad should be docked to bottom of screen
        Adview.frame = CGRectMake(0.0,
                                  self.view.frame.size.height -
                                  Adview.frame.size.height,
                                  Adview.frame.size.width,
                                  Adview.frame.size.height);
    }];

This updates the frame of the Adview and makes it show at the bottom of the screen, working as expected.

The issue I have is with the UITableView frame. If I update the frame in the same method it appears to update again I believe from the previously set constraints on the table view in the Storyboard.

What would be the correct way to change the constraints/frame of the UITableView so that if an Ad is received or not the relevant changes apply.

Receive - -(void)adView:(GADBannerView *)Adview didFailToReceiveAdWithError:(GADRequestError *)error {
Failure - - (void)adViewDidReceiveAd:(GADBannerView *)Adview

Was it helpful?

Solution

The better way is to layout your view with the ad frame showing. Have a constraint between the TableView and the adView and then another between the adView and the bottom layout guide. Set an outlet to the bottom constraint.

Then in your code, instead of adjusting frames, set the constant property of the bottom constraint appropriately. Set it to 0 to show the ad, set it to -50 (or your ad fame height) to hide.

[UIView animateWithDuration:1 animations:^{
    self.adViewConstraint.constant = 0;
    self.adView.hidden = NO;
}
[self.view layoutIfNeeded];

Now your tableView should automatically size itself as the adView is hidden or shown.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top