Reputation: 115
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.
Upvotes: 2
Views: 1031
Reputation: 115
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. :)
Upvotes: 1
Reputation: 18878
I believe this is an Auto Layout issue for ADBannerView
's. I implement my ADBannerView
's in a similar manner and I've been unable to find a way to silence the warning while at the same time satisfying the flexibility required for my ADBannerView
. Implementing an AdMob GADBannerView
in the same exact way does not cause an Auto Layout warning to occur. As you can see in my screen shot, the ADBannerView
will indeed be stretched across the devices screen, width = 600
, but Auto Layout still thinks it is at the default size, width = 480
.
The best answer at this time is to simply ignore the warning.
Upvotes: 0