Resize Layout according to banner view's appearance

| | August 10, 2015

I have an app that is currently published on AppStore. Now I am trying to change my advertisement strategy. I am trying to implement an algorithm like ” show iAd, if it fails show admob”
I followed the code iAd Admob mediation with singleton
I have a problem with implementation below. Everything seems fine except banner view is overlaying my main view. As I said before this app was distributed so far, so I am trying to do it without changing my previous layout and auto layout constraints. I want my previous view will auto resize according to appearance of banner view. When banner will appear, main view should shrink then will enlarge if banner cannot get any ad.

It overlays my main view elements.
It overlays my main view elements

AppDelegate.swift

var iAdBanner = ADBannerView()
var adMobBanner = GADBannerView(adSize: kGADAdSizeBanner)

//Banner View Code

func bannerViewDidLoadAd(banner: ADBannerView!) {

    UIView.beginAnimations(nil, context: nil)
    iAdBanner.alpha = 1.0
    adMobBanner.alpha = 0.0
    UIView.commitAnimations()
    NSLog("iAd loaded ad")
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {


    UIView.beginAnimations(nil, context: nil)
    iAdBanner.alpha = 0.0
    adMobBanner.alpha = 1.0
    UIView.commitAnimations()
    NSLog("iAd failed to load ad")

}

func adView(view: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {

    println("We dont have admob hide it!")

    adMobBanner.alpha = 0
}

func adViewDidReceiveAd(view: GADBannerView!) {

    if iAdBanner.alpha == 0.0 {
        println("No IAD show admob")
        adMobBanner.alpha = 1.0

    }
}

Registration.swift

class Registration: NSObject {

var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate //non-optional variable
static var sharedInstance = Registration()

func showAds(viewController:UIViewController, view:UIView?) {
    var bannerView:UIView = view ?? viewController.view

    //Admob
    appDelegate.adMobBanner.rootViewController = viewController
    appDelegate.adMobBanner.delegate = appDelegate
    var request = GADRequest()
    appDelegate.adMobBanner.adUnitID = "ca-app"
    appDelegate.adMobBanner.loadRequest(request)
    appDelegate.adMobBanner.setTranslatesAutoresizingMaskIntoConstraints(false)
    bannerView.addSubview(appDelegate.adMobBanner)
    var myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)
    myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)
    myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)

    // iAd
    appDelegate.iAdBanner.delegate = appDelegate;
    appDelegate.iAdBanner.setTranslatesAutoresizingMaskIntoConstraints(false)
    bannerView.addSubview(appDelegate.iAdBanner)
    myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)

    myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)

    myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)

}

I showed my ad in my main view controller like:

    override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    Registration.sharedInstance.showAds(self,view:nil)
}

2 Responses to “Resize Layout according to banner view's appearance”

  1. A simple solution to your problem would be using self.canDisplayBannerAds = true. Simply add it to your viewDidLoad. Implementing your iAd banner in this way will resize your UIView when an ad is available and move itself off screen when an iAd banner is not available. Remove your current implementation of your ADBannerView and give it a try. There are downsides to using self.canDisplayBannerAds = true though, like not being able to use the ADBannerView‘s delegate methods. I’d assume you could check your UIView to see if it contains an ADBannerView or not and act upon it accordingly if you’d like.

  2. You can create a single UIView, say you name it bannerAdView set its size whatever way you want, and show your iAd or Admob inside that view. Give frame size to iAdBanner and adMobBanner same as the bannerAdView's bounds

Leave a Reply