在 AppDelegate 和自动调整视图大小中使用 adMob
我正在将我的付费 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以调整 navigationController.view.frame 的大小,以便 adMobAd 不会阻止内容。
本质上将其添加到 didReceiveAd 调用的末尾:
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: