I have Xcode 6.3.2 and I have a problem with implementation of ADBannerView on storyboard. Xcode shows all the time a warning:
Frame for "Banner View" will be different at run time.
I added 3 constraints, you can see them below.
All of constraints
Center horizontally
Bottom space to bottom layout guide = 0
Leading space = 0
How to implement banner correctly?
I can not use "self.canDisplayBannerAds = true" because I also use "bannerViewDidLoadAd" and "didFailToReceiveAdWithError" to resize content and also "bannerViewActionShouldBegin" and "bannerViewActionDidFinish" to pause and start again application activity.
SOLVED!
To add iAd banner using Auto Layout and Size Classes in portrait and landscape but without using canDisplayBannerAds
first declare banner var bannerView: ADBannerView!
.
Use this to set delegate and add banner to view:
func loadAds() {
bannerView = ADBannerView(adType: ADAdType.Banner)
bannerView.hidden = true
bannerView.delegate = self
self.view.addSubview(bannerView)
}
Use following code to let banner rotate with screen and resize screen content contentView
when iAd loads (bottomConstraint
is a constraint from contentView
to bottom):
override func viewDidLayoutSubviews() {
self.layoutAnimated(UIView.areAnimationsEnabled())
}
func layoutAnimated(animated: Bool) {
var contentFrame = self.view.bounds
var sizeForBanner = bannerView.sizeThatFits(contentFrame.size)
var bannerFrame = bannerView.frame
if self.bannerView.bannerLoaded {
contentFrame.size.height -= sizeForBanner.height
bannerFrame.origin.y = contentFrame.size.height
bannerFrame.size.height = sizeForBanner.height
bannerFrame.size.width = sizeForBanner.width
let verticalBottomConstraint = self.bottomConstraint
verticalBottomConstraint.constant = sizeForBanner.height
self.view.layoutSubviews()
bannerView.hidden = false
} else {
bannerFrame.origin.y = contentFrame.size.height
bannerView.hidden = true
let verticalBottomConstraint = self.bottomConstraint
verticalBottomConstraint.constant = 0
}
UIView.animateWithDuration(animated ? 0.25 : 0.0, animations: {
self.contentView.layoutIfNeeded()
self.bannerView.frame = bannerFrame
})
}
Here you call above code to show and hide banner when loads or failed to load iAd
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.layoutAnimated(true)
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.layoutAnimated(true)
}
Now you can use bannerViewActionShouldBegin
and bannerViewActionDidFinish
to pause and start your app activity. :)