4

I want to be able to display an ADBannerView right above my UITabBar (which displays a UITableView). Unfortunately my banner is not positioning correctly. It will appear right below the UITableView, and then as soon as I scroll, the banner will remain in the middle of my UITableView.

I want the banner to appear right above the UITabBar, and allow the UITableView to scroll behind the banner when the user drags.

-(void)layoutForCurrentOrientation:(BOOL)animated
{
    CGFloat animationDuration = animated ? 0.2 : 0.0;
    // by default content consumes the entire view area
    CGRect contentFrame = self.view.bounds;
    // the banner still needs to be adjusted further, but this is a reasonable starting point
    // the y value will need to be adjusted by half the banner height to get the final position
    CGPoint bannerCenter = CGPointMake(CGRectGetMidX(contentFrame), CGRectGetMaxY(contentFrame));
    CGFloat bannerHeight = 0.0;

    // First, setup the banner's content size and adjustment based on the current orientation
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
        bannerHeight = 32.0;
    }
    else
    {
        banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
        bannerHeight = 50.0;
    }

    // Depending on if the banner has been loaded, we adjust the content frame and banner location
    // to accomodate the ad being on or off screen.
    // This layout is for an ad at the bottom of the view.
    if(banner.bannerLoaded)
    {
        contentFrame.size.height -= bannerHeight;
        bannerCenter.y -= bannerHeight / 2.0;
    }
    else
    {
        bannerCenter.y += bannerHeight / 2.0;
    }

    // And finally animate the changes, running layout for the content view if required.
    [UIView animateWithDuration:animationDuration
                     animations:^{
                         self.tableView.frame = contentFrame;
                         [self.tableView layoutIfNeeded];

                         banner.center = bannerCenter;
                     }];
}
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • Curious if you were able to resolve this without adding a parent view. – Nick Feb 07 '11 at 23:27
  • I'm bumping into the same problem - the tableview doesn't adjust properly and ends up behind the ADBannerView - did you manage to solve this ? – ferdil Jun 06 '11 at 19:12

2 Answers2

2

I figured out how to do that for usual UITableViewController without using custom UIViewController with UITableView inside. The trick is to dynamically adjust position of banner view to make it always be on the bottom of visible area.

You have just add your banner view as subview and adjust table insets accordingly:

- (void)viewDidLoad 
{
        [super viewDidLoad];

        ...

        //init banner and set it's frame (here we use 320x50 banner) 
        self.bannerView = [[UIView alloc] init];
        self.bannerView.frame = CGRectMake(0, self.view.frame.size.height - 50, 320, 50);

        //add as subview
        [self.view addSubview:self.bannerView];

        //set proper bottom inset depending on your banner height
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
}

When the table is scrolled you need to adjust banner position depending on content offset:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //refresh banner frame during the scrolling
    CGRect bannerFrame = self.bannerView.frame;
    bannerFrame.origin.y = self.view.frame.size.height - 50 + self.tableView.contentOffset.y;
    self.bannerView.frame = bannerFrame;
}

My example is for bottom-positioned banner but it's easy to change calculations to apply it for banner placed at the top of table.

xZenon
  • 655
  • 6
  • 18
0

You should be creating a view that holds your tableview and the banner ad as separate entities, rather than resorting to the hackery above. That is, create a new UIView that will be set to your view controllers 'view' property. In this view, put your banner ad just off screen (its y origin should be negative the height of the banner), and your tableview's origin should be 0,0. Once the banner loads successfully, animate the banner into origin 0,0 and also move the tableview's origin to 0,bannerHeight.

This will give you the effect you'll see in the WWDC10 video on iAds, which I recommend watching if you haven't already.

jer
  • 20,094
  • 5
  • 45
  • 69