How to Add an Admob Gadbannerview to Every View

AdMob GADBannerView delay when presenting new VC

Looks like you're trying to create one GADBannerView to use across your entire application, which is a good idea. You're not doing that though because you're creating a new GADBannerView in your View Controller's requestAds function: appDelegate.adBannerView = GADBannerView(). Remove it.

Also, do your request and set your Ad Unit ID in your app delegate and remove it from your requestAds function. In your View Controller's viewDidLoad you should only be positioning your GADBannerView and adding it to the view.

I imagine you would like the banner to fill the width of the screen too so I've changed the frame property. If you wanted a function for this it would end up similar to:

func addBannerToView() {
appDelegate.adBannerView.adSize = kGADAdSizeBanner
appDelegate.adBannerView.rootViewController = self
appDelegate.adBannerView.frame = CGRect(x: 0.0,
y: view.frame.height - appDelegate.adBannerView.frame.height,
width: view.frame.width,
height: appDelegate.adBannerView.frame.height)
view.addSubview(appDelegate.adBannerView)
}

Add GADBannerView Programmatically

Well I think you have multiple things to consider:

  1. [sender.view addSubview:bannerView] does not move other views,
    instead you are placing the banner view directly on top of the view
    hierarchy.

  2. Ads are loaded asynchronously, so you should implement the delegate
    methods of GADBannerView for showing and hiding the ad view.

  3. If you really want to move other views around you should use some
    kind of container view to include all other content except your
    banner. If you receive an ad you can easily move the container view
    (or shrink it) and display your banner view on some location in your
    main view.

Cheers



Related Topics



Leave a reply



Submit