문제

I would like to add a fixed GADBannerView (Google Mobile Ads) on the bottom of the screen inside my UITableViewController: I have found tutorials on how to incorporate it into a section header, footer, cell etc.. but I want the banner to be fixed even if the user scrolls. As you probably know, you can't directly resize an UITableView placed in a UITableViewController.

Is there anyway other than re-creating the ViewController in a different manner? Maybe placing it in a Toolbar could solve the problem? Anyone used this method?

Thank you!

도움이 되었습니까?

해결책

I solved with the following (good in my opinion) solution:

[self.navigationController setToolbarHidden:NO];
self.banner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
self.banner.adUnitID = @"ca-app-pub-XXXXXXXX/XXXXXXXXX";
self.banner.rootViewController = self;
[self.navigationController.toolbar addSubview:self.banner];

GADRequest *request;
request.testDevices = @[GAD_SIMULATOR_ID];
[self.banner loadRequest:request];
  1. Make sure the toolbar is visible
  2. Add the GADBannerView to the toolbar.

다른 팁

At I added admob to my app a few weeks ago (in addition to iAd), but maybe this will help you.

I used this code to place it in my UITableViewController:

// defining the BannerView
self.bannerView_ = [[GADBannerView alloc]
               initWithFrame:CGRectMake(0.0,
                                        self.view.frame.size.height -
                                        GAD_SIZE_320x50.height,
                                        GAD_SIZE_320x50.width,
                                        GAD_SIZE_320x50.height)];

// Specify the ad unit ID.
self.bannerView_.adUnitID = @"xxxxxxxxxxxxxxxxxxxxx";

// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
self.bannerView_.rootViewController = self;
[self.view addSubview:self.bannerView_];

And to keep it at the position when scrolling I use this:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGRect iAdFrame = self.iAdBannerView.frame;
    CGFloat newOriginY = self.tableView.contentOffset.y + self.tableView.frame.size.height - iAdFrame.size.height;
    CGRect newIAdFrame = CGRectMake(iAdFrame.origin.x, newOriginY, iAdFrame.size.width, iAdFrame.size.height);
    self.iAdBannerView.frame = newIAdFrame;
    self.bannerView_.frame = newIAdFrame;
}

Scrolling the banner view with the tableview as @VaaChar proposes is definitely not recommended considering the performance.

I solved the same problem by converting the UITableViewController to a UIViewController and adding the tableview as subview to the view of this view controller.Instead of adding the banner to tableview you can add it to you view and resize the tableview according to the height of the banner.

Since the banner appears with some delay, in order to keep the table view in full size till banner appears i change the height of the tableview in the delegate method

- (void)adViewDidReceiveAd:(GADBannerView *)view{

    if (!self.adRecieved)
    {
        CGRect currentRect = self.tableView.frame;
        currentRect.size.height -= CGRectGetHeight(view.frame);
        [self.tableView setFrame:currentRect];
        self.adRecieved = YES;
    }

}

since the method is called multiple time as he banner refreshes self.adRecieved is a boolean variable to keep track of the first update.

For everyone who is looking for the Swift version of the code.

  1. I created a new UIView within the first section of the Tableview Cell. This UIView has no constraints or a width/height because this was not possible to be set on this top position within the Storyboard.

Additionally i added the following code in my viewDidLoad() it worked for me by using swift:

            var bannerView: GADBannerView!
            addBannerViewToView(bannerView)
            bannerView.adUnitID = "ca-app-pub-XXXXXXXX/XXXXXXXXX"
            bannerView.rootViewController = self
            navigationController?.toolbar.addSubview(bannerView)
            bannerView.load(GADRequest())
            bannerView.delegate = self
            view.exerciseAmbiguityInLayout()

And this within my class:

func addBannerViewToView(_ bannerView: GADBannerView)
{
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
        [NSLayoutConstraint(item: bannerView,
                            attribute: .bottom,
                            relatedBy: .equal,
                            toItem: bottomLayoutGuide,
                            attribute: .top,
                            multiplier: 1,
                            constant: 0),
         NSLayoutConstraint(item: bannerView,
                            attribute: .centerX,
                            relatedBy: .equal,
                            toItem: view,
                            attribute: .centerX,
                            multiplier: 1,
                            constant: 0)
        ])
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top