2013-09-10 61 views
4

我正在尋找在我的應用程序中添加AdMob橫幅。我已經將它們放置在每個屏幕的底部,並且它可以正常工作,但並不完全是UIViewControllers。如何將靜態廣告橫幅添加到UITableViewController中?

當我把一個添加到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]); 
} 

我已經看到了多個例子是,我怎麼可以嵌入廣告插入到表視圖,但沒有什麼具體的我要怎麼做。我唯一讀到的就是我應該將表視圖放入一個容器視圖中,然後將該廣告添加到該視圖中。我不知道我會怎麼做,但是,因爲這是一個UITableViewController。

回答

3

我發現最簡單的方法是通過不使用專用的UITableViewController。我創建了一個UIViewController並將一個容器控制器添加到視圖中。從那裏我將容器控制器分類爲UITableViewController。我只是把我所有的代碼都與這個子類中的表視圖相關聯。然後我將廣告的加載和放置放置在頂層的UIViewController中。這樣做意味着廣告被嵌入到與容器控制器相同的視圖中。我只是把它做成了讓我的廣告橫幅位於容器的頂部。這導致我可以滾動表格視圖,並且廣告橫幅不會移動。

+0

你可以把這個代碼。我也需要它,但我是iOS編程的新手,很難從描述中去追蹤。 – WebOrCode