global ADBannerView in iPhone app

TomSwift picture TomSwift · Sep 8, 2010 · Viewed 9k times · Source

Is it possible with a standard UINavigationController-rooted app, to have a single ADBannerView visible at the bottom of the screen, below the view hierarchy? That is, without modifying each view-controller/view that can be pushed to the root UINavigationController, can I have a global ADBannerView be visible?

I'm not sure how to set this up, either in IB or in code. Help?

I see similar questions with vague answers. I'm looking for a concrete example.

Answer

TomSwift picture TomSwift · Sep 8, 2010

EDIT: The better way to do this in iOS5+ is likely to use view controller containment. That is, make a root controller that contains your ad and application controller (nav, tab, etc.).

I figured out a way to do this. Here is what I did:

For my first attempt I created a new view controller called AdBannerController. For its view I created a full-screen view and two subviews. The first subview (contentView) is for regular content, the second is the AdBannerView. I used an instance of this view controller as the view controller associated with the app window ( [window addSubview: adBannerController.view] ). Then I added my UINavigationController.view as a subview of adBannerController.view: [adBannerController.contentView addSubview: navigationController.view].

This mostly worked except that viewcontrollers pushed to the UINavigationController never got their will/did-load/unload methods called. Shucks. I read in a few places that this is a symptom of the UINavigationController view not being a direct descendant of the app window.

For my second attempt I took the same AdBannerController and derived it from UINavigationController. This time, I did the following in loadView:

- (void)loadView
{
    [super loadView];

    _contentView = [self.view retain];

    self.view = [[[UIView alloc] initWithFrame: _contentView.frame] autorelease];

    [self.view addSubview: _contentView];

    _adView = [[ADBannerView alloc] initWithFrame: CGRectMake(0, _contentView.bounds.size.height, 320, 50)];
    _adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
    _adView.delegate = self;

    [self.view addSubview: _adView]; 

    /* for visual debugging of view layout
    [[_mainView layer] setCornerRadius: 6.0];
    [[_mainView layer] setMasksToBounds: YES];
    [[_mainView layer] setBorderWidth: 1.5];
    [[_mainView layer] setBorderColor: [[UIColor grayColor] CGColor]];  
     */
}

Notice what happens - I let the superclass UINavigationController construct its regular "content" view, but I swap it out and replace it with my own view which is a container for both the content and ad views.

This works pretty well. I'm also using three20 and there were a few things required to make this work with that setup, but not too bad.

I hope this helps someone!