iphone - 如何将静态广告横幅添加到 UITableViewController 中?

标签 iphone ios objective-c uitableview

我希望在整个应用中添加 AdMob 横幅。我已将它们放在每个屏幕的底部并且它可以正常工作,但对于 UIViewControllers 来说并不是完全正确。

当我将一个 add 放到 UITableViewController 上时,它从屏幕底部的正确位置开始,但是当我滚动表格时,它会随之移动。当我滚动表格时,我需要广告静态地停留在屏幕底部。

这是我的代码:

- (void)displayGAD
{
    // The frame of the banner is initialized off screen.
    // If an ad loads then it will animate onto the screen.
    self.bannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,
                                                                      self.view.frame.size.height,
                                                                      GAD_SIZE_320x50.width,
                                                                      GAD_SIZE_320x50.height)];

    self.bannerView.adUnitID = self.adUnitID;
    self.bannerView.delegate = self;
    self.bannerView.rootViewController = self;

    [self.view addSubview:self.bannerView];

    [self.bannerView loadRequest:[self createRequest]];

}



- (GADRequest *)createRequest
{
    GADRequest *request = [GADRequest request];

#warning Comment this out before distribution
    request.testDevices = [NSArray arrayWithObjects:@"84ea3d9789cabb0a34176cbb52c0f992", @"abf08fe141b95987d27ac068602605b8", GAD_SIMULATOR_ID, nil];

    return request;
}



- (void)adViewDidReceiveAd:(GADBannerView *)view
{
    NSLog(@"Received Ad");

    [UIView animateWithDuration:1.0 animations:^ {
        view.frame = CGRectMake(0.0,
                                self.view.frame.size.height - view.frame.size.height,
                                view.frame.size.width,
                                view.frame.size.height);
    }];
}

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
    NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
}

关于如何将广告嵌入到表格 View 中,我已经看到了多个示例,但没有任何关于我想要如何做到这一点的具体例子。关于此,我唯一读到的是我应该将 TableView 放入容器 View 中,然后将广告也添加到其中。不过,我不知道该怎么做,因为这是一个 UITableViewController。

最佳答案

我发现最简单的方法是不使用专用的 UITableViewController。我创建了一个 UIViewController 并向 View 添加了一个容器 Controller 。从那里我将容器 Controller 子类化为 UITableViewController。我只是将所有与 TableView 相关的代码放在该子类中。然后,我将广告的加载和放置放在顶层 UIViewController 中。这样做意味着广告嵌入到与容器 Controller 相同的 View 中。我只是这样做,以便我的广告横幅位于容器的顶部。这导致我能够在表格 View 和广告横幅不动的情况下滚动。

关于iphone - 如何将静态广告横幅添加到 UITableViewController 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18728060/

相关文章:

ios - 如何仅使用 Storyboards 呈现弹出窗口

iphone - 在主 `UITableViewCell` 已经加载后,如何用 `UIScrollView` 或 `UITableView` 替换 `UITableView`?

iphone - 在类之间传递NSString

objective-c - 如何在命名空间中声明 Objective-C 中的常量?

ios - 如何将变量放入文本字​​段以及如何将所有元素转换为字符串而不仅仅是一次 1?

ios - 使用 Xcode 4.6 获取 IOS 项目的 UML 类图

ios - Swift 将 TableView 分成两部分

ios - 在 iOS 8 扩展中访问 Core Data SQL 数据库(在 App 和 Widget 扩展之间共享数据)

iphone - 出于测试目的在应用程序中设置 iPhone 日期/时间?

iphone - HTML5音频:设置currentTime,然后在iPhone上自发倒带50-100毫秒