user2872856 user2872856 - 2 months ago 45
iOS Question

How do I position an Admob Smart Banner?

I created a 320x50 banner ad using this code:

var bannerView = GADBannerView(frame:CGRectMake(0, 20, 320, 50))


The ad should have y-position of 20 to leave a space for the status bar. Now, I would like to change to using a Smart Banner. So I changed my code to:

let bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)


Obviously the above code covers the status bar. I have no idea on how to make the Smart Banner appear at a y-position of 20.

Answer

Just change your bannerView's frame:

let bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
bannerView.frame = CGRect(x: 0.0,
                          y: UIApplication.sharedApplication().statusBarFrame.size.height,
                          width: bannerView.frame.width,
                          height: bannerView.frame.height)

Also, setting static positions is usually never a good idea. You can get the status bar's height with UIApplication.sharedApplication().statusBarFrame.size.height. This way, if the status bar height ever changes you don't have to worry about it.