Moshe
Moshe

Reputation: 58087

iOS 7's canDisplayBannerAds and rotation?

I've installed iAd banners in my app using the new iOS 7 property on UIViewController, namely canDisplayBannerAds. The trouble is that my view no longer resizes correctly on iOS 7. I'm using autolayout, in case you were wondering.

In portrait, the ad banner appears at the bottom of the screen. In landscape, the UI doesn't properly recenter, and remains as tall as a portrait screen. (My buttons and ad banner, which are constrained to the bottom of the screen, are truncated.) Essentially, the bounds of my view no longer updates correctly.

I've attempted to hide the disable canDisplayBannerAds in willRotateToInterfaceOrientation and then re-enable the property in didRotateFromInterfaceOrientation. No dice.

I've manually traversed the view hierarchy to find the ad banner and call sizeThatFits on it. I'm not confident that I did this correctly, so this may be the answer.

I've tried explicitly setting autoresizing masks on the view that becomes the originalContentView before enabling ads.

Removing my call to canDisplayBannerAds alleviates all symptoms of the problem.

Am I missing something obvious? Is there another property or setting that I'm supposed to be toggling? Is this a bug?

EDIT:

I'm not actually installing the banner myself. Simply setting the canDisplayBannerAds to YES causes iOS to resize my entire hierarchy to make room for a banner ad which it then installs by itself.

Upvotes: 5

Views: 600

Answers (3)

user2191247
user2191247

Reputation:

It is a bug. Got a workaround courtesy of Yimin Rong.

If you add the following two lines in viewDidLoad:

[self setCanDisplayBannerAds: YES];
[self setCanDisplayBannerAds: NO];

Then the ads will be set up properly in your app, but won't be displayed. It has to be in viewDidLoad, otherwise the ads won't be set up right and you'll get layout issues or exceptions generated.

Then you can call [self setCanDisplayBannerAds: YES]; when you're ready. This doesn't have to be in viewdidLoad, it can be anywhere! Works for iOS 8 as well, which has the same bug.

Upvotes: 1

Pochi
Pochi

Reputation: 13459

I have seen other posts talking about undefined behavior when setting "canDisplayBannerAds" to display ads.

I believe it was due to custom views inside their view hierarchy, do you have any?

Still, in my opinion you are running into too much trouble and should simply add the banners the old way:

ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierLandscape];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
[self.view addSubview:adView];

(For landscape views, you can change the type on the view will rotate to support portrait)

Am I missing something obvious? Is there another property or setting that I'm supposed to be toggling? Is this a bug?

There is not another property, if you want to only enable ads by setting that property you cannot get delegate calls either.

What you have to do is, have the viewcontroller's MAIN view properly set up for resizing, (you can test with the interface builder).

Edit:

This person seems to have solved it by adding more constrains canDisplayBannerAds Issue when Starting in Landscape Mode

Are you sure your view is already properly resizing? do you have the same issue with the ads in a blank project?

(my advice is still the same though, adding them manually gives you much more freedom and you can place them wherever you want. My app shows "portrait ads" while in landscape, for example.)

Upvotes: 1

BHASKAR
BHASKAR

Reputation: 1201

You have to set the constraint on your bannerview if you add it programmatically like this

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yourbannerview
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:0.5
                                                       constant:0]];

and so on.... :) You can follow this link

http://matthewmorey.com/creating-uiviews-programmatically-with-auto-layout/

Upvotes: 0

Related Questions