views:

196

answers:

1

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.

+2  A: 

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!

TomSwift
Very helpful, I'm trying this now.
bentford
i implemented something like this but had difficulties in views where I am using a UISearchBar as the tableViewHeader, as well as in views where the navBar is transparent
coneybeare