Search code examples
swiftiadadbannerview

How to make close button for iAdbannerView


Can anybody help me in making a close button for iAdbannerview which will pe place above the banner in right topside so that if person do not want to see the ads he/she can tap on close button to hide the ads.Help me in coding Below is the link to open screenshot of my viewcontroller https://drive.google.com/file/d/0B2z_d4wEKPEFTHVNY1AycVdTNTQ/view?usp=sharing

  @IBOutlet var adBannerView: ADBannerView?

 override func viewDidLoad() {
super.viewDidLoad()




self.canDisplayBannerAds = true
self.adBannerView!.delegate = self
self.adBannerView!.hidden = true //hide until ad loaded

 }

func bannerViewWillLoadAd(banner: ADBannerView!) {

  }

func bannerViewDidLoadAd(banner: ADBannerView!) {

self.adBannerView?.hidden = false

 }

func bannerViewActionDidFinish(banner: ADBannerView!) {

  }

   func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplicationwillLeave: Bool) -> Bool {

return true
}

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

Solution

  • This is the way to do it programmatically :

    Create a UIButton under adBannerView:
    var button = UIButton()

    In viewDidLoad instantiate the button and place it at the right top side of the adBannerView :

    button.frame = CGRectMake(self.view.center.x + 150, self.view.frame.size.height - 66, 10, 10)
    button.setBackgroundImage(UIImage(named: "Yourclosebuttonimagename"), forState: UIControlState.Normal)
    self.view.addSubview(button)
    button.addTarget(self, action: "hideAd:", forControlEvents: UIControlEvents.TouchUpInside)
    

    The function will be called hideAd :

    func hideAd(sender:AnyObject){
    
    self.adBannerView.hidden = true
    self.button.hidden = true
    // or do 
    self.button.removeFromSuperview()
    self.adBannerView.removeFromSuperview()
    }
    

    I recommend you remove self.canDisplayBannerAds = true because self.adBannerView!.delegate = self is enough to run the iAd.

    Good luck !