3

I'd like to place an ADBannerView object onto my UITableView screen statically, what means that I want it to always stay above my toolbar (self.navigationController.toolbar), even when the user is scrolling the tableview. I've solved this by adding by ADBannerView as a subview to my toolbar and given it negative values for the frames origin:

[self setBannerViewSize];
[self.navigationController.toolbar addSubview:bannerView];

The only problem is: I can't click and open the iAd this way - I can see the banner but nothing happens when I tap on it.

Since I'm also using a refreshControl, the option to use a UIViewController instead of UITableViewController and add a tableView manually wouldn't work for me. Is there any other way I can get my ADBannerView statically showing in my table view controller AND still being tappable?

Thank you in advice!

CGee
  • 1,650
  • 5
  • 20
  • 31

3 Answers3

8

Yay!! After all I succeeded in solving this (really annoying) problem by myself (and a lot of reading around)!

First, I found this really world-changing post. Basically this post handles with the topic that a UITableViewController uses self.view for its tableView property, so overriding the tableView property (or synthesizing it manually) plus giving self.view a new view (from application) and adding tableView as its subview would make it possible to reach the real superview of tableView.

But this still didn't solve my problem, although I was sure it would, because it all made sense. My bannerView appeared in the right place (and was fixed) but it still didn't do anything when clicked. But there was a second minor thing I didn't know about:

As I read in this post the superview of a subview doesn't only have to be userInteractionEnabled but also have a non-transparent backgroundColor. Because my superviews background color was set to [UIColor clearColor] it all didn't work - but setting its backGroundColor to e.g. blackColor solved the whole problem: the bannerView got finally tappable! :)

So, my code is now looking like this:

@synthesize tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (!tableView && [self.view isKindOfClass:[UITableView class]]) {
        tableView = (UITableView *)self.view;
    }

    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];

    [self resizeTableToFitBanner];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bannerView];

    // some other code
}
CGee
  • 1,650
  • 5
  • 20
  • 31
  • world-changing post ;) JK -- cool you got it to work, the code looks good BUT I would not set the insets I guess (0 should be the default?) and i would inline setBannerViewSize i guess... make it more explicit and easier to understand – Daij-Djan Nov 29 '12 at 21:54
  • now that I think of it. I would just not subclass UITableViewController but UIViewController... would save you half the code ;) => myVC : UIViewController and done you are – Daij-Djan Nov 29 '12 at 21:56
  • Yeah, thanks for the suggestion with the 0, I'll edit this. Inlining my setBannerViewSize would bless the code up, since it's quite complicated how I do that. But to the UIViewController: as I posted above I also use a refreshControl, which currently only work in UITableViewControllers ... :) – CGee Nov 29 '12 at 22:14
  • @Dschee, thanks for the link to the "world-changing" post. I found that I could do the same thing in the storyboard. I deleted the table view from the table view controller and added a UIView. Set the controller's view property to that view, and then added a table view as the subview of that view. I still needed to add the tableView property in the .h and connect that up in IB. – rdelmar Nov 29 '12 at 22:34
  • 1
    @Dschee k, then Id rename the setBannerSize to ... [self resizeTableToFitBanner] or so :D setBanneViewSize implies its a setter for a property (IMO! only) – Daij-Djan Nov 29 '12 at 22:34
  • rdelmar, thanks, good to know. :) @Daij-Djan: You're right, I'll do it the way you suggested. ;) – CGee Nov 30 '12 at 12:31
  • @Daij-Djan because UITableViewController has a lot of required functionality for the table than the basic UIViewController. Learn about 8 of the features by googling for "Recreating UITableViewController". – malhal Mar 03 '14 at 06:48
  • Replacing the table controller's table with a parent view is not a good way to do this. See the iAdSuite by Apple for a much better solution. – malhal Mar 03 '14 at 22:54
2

BannerViewController in Apple's iAdSuite sample code solves this problem very elegantly:

https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html

malhal
  • 26,330
  • 7
  • 115
  • 133
0

I think you should use a container view, and set things up in IB. You can add a tool bar and ADBannerView to the bottom of the view of your navigation controller's root view controller. Fill the rest of the space with a container view - this will give you an embedded view controller automatically. You should delete this one and then drag in a tableViewController and control drag from the container view to the tableViewController to hook up the embed segue.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thanks for this suggestion, but I don't use any IB at all, I do all programmatically. And I solved the problem in an other way, I'll describe it in a minute. – CGee Nov 29 '12 at 21:32