Search code examples
iosobjective-cuitoolbariadadbannerview

iOS iAd Integration Issue With UIToolBar


I am trying to add iAd into my project. The view controller that i am trying to iAd into contains a UIWebView. A UIToolBar is also added at the bottom. Above the tool bar, i have dragged a ADBannerView inside my StoryBoard. This is how it looks like:

ios iad integration

To show the ads, this is what i have done so far: Added iAd frameWork, created an IBOutLet from AdBannerView named "banner", in viewDidLoad i assigned the delegate to self. Then i added the AdBannerViewDelegate methods and also added the following method:

- (void) viewDidLayoutSubviews {
    if (self.banner.bannerLoaded) {
        CGRect contentFrame = self.view.bounds;
        CGRect bannerFrame = self.banner.frame;
        contentFrame.size.height -= self.banner.frame.size.height;
        bannerFrame.origin.y = contentFrame.size.height;
        self.banner.frame = bannerFrame;
    }
}

Well the iAd is properly showing with all the above i have done.

PROBLEM: The first time the view is loaded, it shows the ads properly but when i unload and reload the view again, UIToolBar disappears and is covered by the AdBannerView which is shifted below in place of UIToolBar. Can anyone points out where the problem could be and how to solve it? Thanks.


Solution

  • Your problem is due to setting frames in the viewDidLayoutSubviews method. When using auto layout, you shouldn't set any frames. In the storyboard, when you add the addBannerView, give it a height constraint, and spacing constraints to the to sides of the view, and a vertical spacing constraint to the tool bar (the web view should also have a vertical spacing constraint to the top of the add banner view). Delete the viewDidLayoutSubviews method, and it should work properly.