在AppDelegate中使用adMob并自动调整视图大小

时间:2010-11-06 01:26:09

标签: iphone admob autosize

我正在将付费的iPhone应用转换为免费的应用,并使用AdMob集成

为了简化集成,我将AdMobView添加到AppDelegate。

这一切都很好,因为它在屏幕底部显示广告。但不幸的是,它掩盖了之前在底部显示的内容,这也是后续视图被推送到navigationController的情况。有没有办法让Interface Builder将内容“挤压”在一起,以适应底部的广告视图,同时让所有其他按钮和视图可见?

以下是adMob集成的AppDelegate代码的子集:

- (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];
}

1 个答案:

答案 0 :(得分:0)

您可以调整navigationController.view.frame的大小,以便adMobAd不会阻止内容。

基本上将此添加到didReceiveAd调用的末尾:

CGRect navFrame = navigationController.view.frame;
navFrame.height -= 48;
navigationController.view.frame = navFrame;