1

I have successfully implemented iAd in Swift, except when an ad appears, it reconfigures the dimensions of self. How do I make it such that the ad appears in front of the screen, and doesn't alter the size of the screen?

This is the code I used:

class GameViewController: UIViewController, ADBannerViewDelegate {

    var bannerView:ADBannerView?

    override func viewDidLoad() {
         super.viewDidLoad()

         super.viewDidLoad()
         self.canDisplayBannerAds = true
         self.bannerView?.delegate = self
         self.bannerView?.hidden = true
    }
    func bannerViewDidLoadAd(banner: ADBannerView!) {
         self.bannerView?.hidden = false
    }

    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
         return willLeave
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
         self.bannerView?.hidden = true
    }
}

How do I make it such that the ad appears in front of the screen, and doesn't alter the size of the screen?

tdh
  • 884
  • 1
  • 11
  • 22
  • If you use the automatic banner behaviour `self.canDisplayBannerAds = true` you don't need to add another banner. The automatic behaviour will resize the view controller's view (hence triggering auto layout ) and place the banner at the bottom of the _window_. If you need something a little different, here is a small example of mine [iAd simple example](https://github.com/perlfly/iAdExample). It creates a singleton banner and then lets you add the banner at the top or bottom of the view controller's view. It is just an example, but might be a starting point for your needs. – Matteo Piombo Jan 20 '15 at 10:00
  • @perlfly Thank you for your contribution. I do already have iAd implemented, but my problem is that, as you reference, the ad triggers Auto-Layout and makes the game-screen itself smaller. I want the ad to come up in front of the screen, and therefore not affect the layout of the screen. – tdh Jan 21 '15 at 03:42
  • To avoid your view controller's resizing, simply do not set `self.canDisplayBannerAds = true` (it is false by default). Then manually add the banner view where you need it. I encourage you to use a singleton bannedView. Notice that the way you instantiate the bannerView is not correct. See the example mentioned before for a way to instantiate it and add it to a controller's view. – Matteo Piombo Jan 21 '15 at 04:54
  • @perlfly I apologize, it seems I misunderstood your answer before. Thanks! – tdh Jan 21 '15 at 05:01

1 Answers1

1

If you want to use self.candisplaybannerads = true without having the other items move on the screen make sure your items are not constrained to the bottom layout guide. When the ad loads using self.candisplaybannerads = true it moves up the location of the bottom layout guide so any items constrained to this will be moved. If your adding items programmatically you can setting the frame and they should not move when the ad loads. Or if you are using IB and constraints make sure your items are not constrained to the bottom layout guide.

Jason Bestor
  • 99
  • 1
  • 2
  • 10