Perfecting the look of your application is challenging, and incorporating ads into your application requires careful thinking. When using the AdMob SDK with Table Views in iOS, a typical challenge is that ads do not stay docked properly to a certain position on the screen. This blog post outlines a simple solution to this common problem.
Challenge
Consider a simple application in which a UITableViewController is used to handle a list mechanism. If this is the main top level view in the application, getting the ad to remain docked to the bottom or top of the view while the content scrolls is problematic. This is usually because the ad has been placed inside the Table View .
Solution
One way to dock the ad is to encompass the Table View into a container view (e.g. a UIView ). This way, instead of inserting the ad into the Table View as a subview, it’s inserted into the encompassing container view (as a sibling of the Table View ). Here is an example of the code snippet in the header file:
@interface AdsOutOfTableView : UIViewController {
@private
UITableView *tableView_;
//Other private variables here
}
@property(nonatomic,retain) IBOutlet UITableView *tableView;
@end
As shown in this example , the GADBannerView is initialized with a frame that has an origin at the bottom of the screen.
Now when the ad is received, it should be added into the container view, and the size of the Table View should be modified accordingly. The height of the Table View 's frame cannot be modified directly, but this can be done using an intermediate frame as shown below.
-(void)resizeTable
{
CGRect tableFrame = tableView_.frame;
tableFrame.size.height = tableFrame.size.height -
bannerView_.frame.size.height;
tableView_.frame = tableFrame;
}
Now, no matter how much the Table View is scrolled, the GADBannerView stays docked to the bottom of the screen.
Alternatively, keeping the ad docked to the top of the Table View is very similar to the situation shown above. The only difference is that the GADBannerView must be created and drawn at the top of the screen. The resizeTable: method now also has to to shift itself down by the height of the ad:
CGRect tableFrame = tableView_.frame;
tableFrame.size.height = tableFrame.size.height -
bannerView_.frame.size.height;
tableFrame.origin.y = bannerView_.frame.size.height;
tableView_.frame = tableFrame;
With those minor tweaks, you will have ads set up correctly alongside your Table View ! If you have any questions about this or any other SDK topics, feel free to post them in our forum .
- Raj Parameswaran , AdMob Team