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

发布于 2024-10-01 01:51:11 字数 1482 浏览 8 评论 0原文

我正在将我的付费 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];
}

I am converting my paid iPhone app to a free one, with AdMob integration

To simplify integration, I am adding the AdMobView 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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

数理化全能战士 2024-10-08 01:51:11

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

本质上将其添加到 didReceiveAd 调用的末尾:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文