Using adMob in AppDelegate and auto sizing views
I am converting my paid iPhone app to a free one, with AdMob integration
To simplify integration, I am adding the AdMo开发者_开发问答bView to the AppDelegate.
This all works fine, as it is displaying an ad in the bottom of the screen. But unfortunately it covers up the content that was previously shown in the bottom, and this is also the case for subsequent views being pushed to the navigationController. Is there a way to get Interface Builder to "squash" the content together, to fit the ad view at the bottom, while letting all other buttons and views be visible?
Here is a subset of the AppDelegate code with adMob integration:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MyViewController *viewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[viewController release];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
// Request an ad
adMobAd = [AdMobView requestAdWithDelegate:self]; // start a new ad request
[adMobAd retain]; // this will be released when it loads (or fails to load)
return YES;
}
- (UIViewController *)currentViewControllerForAd:(AdMobView *)adView {
return navigationController;
}
// Sent when an ad request loaded an ad; this is a good opportunity to attach
// the ad view to the hierachy.
- (void)didReceiveAd:(AdMobView *)adView {
// get the view frame
CGRect frame = self.window.frame;
// put the ad at the bottom of the screen
adMobAd.frame = CGRectMake(0, frame.size.height - 48, frame.size.width, 48);
[navigationController.view addSubview:adMobAd];
}
You could just resize the navigationController.view.frame so that the adMobAd doesn't block the content.
Essentially add this to the end of your didReceiveAd call:
CGRect navFrame = navigationController.view.frame;
navFrame.height -= 48;
navigationController.view.frame = navFrame;
精彩评论