1

I have something strange in my app and since I'm a beginner with ADBannerView I hope somebody could help me.

I already configured in appDelegate.swift the methods to create and manage the ADBannerView, in my view I add this code:

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewDidLoad() {
        super.viewDidLoad()


        appDelegate.adBannerView.center = CGPoint(x: view.frame.midX, y:  view.frame.height - appDelegate.adBannerView.frame.height / 2)

        view.addSubview(appDelegate.adBannerView)

        self.canDisplayBannerAds = true

    }

Then iAd is correctly shown in my view but also a 49px empty bar over it (the white one). How can I delete it? Is it part of the iAd? Any idea?

Click to see the picture

Thank you in advance!

  • How are you instantiating your `bannerView`? – pbush25 Oct 28 '15 at 16:25
  • in "AppDelegate" i added "var adBannerView = ADBannerView()". adBannerView.delegate = self, adBannerView.hidden = true and the functions bannerViewDidLoadAd, bannerViewActionDidFinish, bannerView. in my view i use let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate , so I can add to the main view appDelegate.adBannerView – towerthepower Oct 28 '15 at 18:10

2 Answers2

2

Thank you pbush25, I found the solution. I post it if others need it.

In appDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate {

var window: UIWindow?
var adBannerView = ADBannerView()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    adBannerView.delegate = self
    adBannerView.hidden = true
    return true
}


func bannerViewDidLoadAd(banner: ADBannerView!) {
    print("bannerViewDidLoadAd")
    adBannerView.hidden = false
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
    print("bannerViewActionDidFinish")
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    print("didFailToReceiveAdWithError: \(error)")
    adBannerView.hidden = true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
    print("bannerViewWillLoadAd")
}

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

and in every view I want to show iAd

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewDidLoad() {
    super.viewDidLoad()
    self.defaultLoad()
}

override func viewWillAppear(animated: Bool) {
    self.defaultLoad()
}
override func viewWillDisappear(animated: Bool) {
    appDelegate.adBannerView.delegate = nil
    view.removeFromSuperview()
}

func defaultLoad(){
    appDelegate.adBannerView.frame = CGRectMake(0, (view.frame.height) - 99, (view.frame.size.width), 50) // 50(banner)+49(tab bar)
    appDelegate.adBannerView.delegate = appDelegate
    super.view.addSubview(appDelegate.adBannerView)
    super.view.bringSubviewToFront(appDelegate.adBannerView)
    super.view.layoutIfNeeded()
    super.canDisplayBannerAds = true
}
0

When you instantiate your ADBannerView you should probably instantiate it with a frame:

var adBannerView = ADBannerView(frame: CGRectMake(0, (self.window?.frame.height)! - 50, (self.window?.frame.size.width)!, 50))

And then it should always be at that part of the whole Window of your app. Thus in your viewDidLoad you don't need to set the center, you'll just need to self.view.addSubview(adBannerView) and then probably self.view.bringSubviewToFront(adBannerView)

pbush25
  • 5,228
  • 2
  • 26
  • 35
  • Thank you, i tried but it doesn't change. I use a Tab Bar Controller to switch between my view, could be that a problem? I tried with your suggestion into a view with no "tab bar" and it works right. Seems like the height of the tab bar is duplicated on the top of the iAd – towerthepower Oct 28 '15 at 18:36
  • Then you might try -100 for it's y position. You might also try `self.view.layoutIfNeeded()` which might help to solve some layout problems. – pbush25 Oct 28 '15 at 18:40
  • I managed to understand that if I use more than one canDisplayBannerAds = true , for example in 2 or more tabs, the white bar appears. So i changed my code into super.view.addSubview(appDelegate.adBannerView); super.view.bringSubviewToFront(appDelegate.adBannerView); super.view.layoutIfNeeded(); super.canDisplayBannerAds = true; but I dunno how to reuse the same adBannerView since I settled it to "super" instead of "self". And if I write into the other tab "super.canDisplayBannerAds = true;" I get the white bar in the first tab. – towerthepower Oct 28 '15 at 19:37