iAd Banner Displays With Portrait Orientation Even

2019-06-09 09:59发布

问题:

I have a landscape only app that I am trying to get working on iOS8. I am trying to create an iAd banner that displays across the bottom of the landscape view. However, when the ad banner displays, it displays across the center of the view and in a portrait orientation. Here is how I set up the views:

app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

// Init the Ad View Controller (viewController is defined as a UIViewController)
viewController = [[AdViewController alloc] initWithNibName:nil bundle:nil];

// Create a banner view controller and make the adViewController be the content controller for the bannerViewController
bannerViewController = [[BannerViewController alloc] initWithContentViewController:viewController];

[app.window addSubview:bannerViewController.view]; 

Note that if I test the statusBarOrientation within this code, it shows that the orientation is landscape. Also note that if I change the last line to add the bannerViewController.view to the root view of the window as shown below:

[app.nrViewController.view addSubview:bannerViewController.view];

the result is the same. Specifically, the ad banner displays across the center of the view and in a portrait orientation. Shouldn't the bannerView be displayed in the same orientation as its parent?

回答1:

Seems like a lot of devs have run into this problem, and I didn't find any answers. Here's how I fixed it:

I programmatically add the iAd to my ViewController like this:

ViewController.h:

ADBannerView *_iadView;

Then in (ViewController.m) viewDidLoad:

CGRect adRect = CGRectZero;

//this is what fixed the issue
adRect.size.width = self.view.bounds.size.width; 

_iadView = [[ADBannerView alloc] initWithFrame:adRect];

CGRect adFrame = _iadView.frame;
adFrame.origin.y = -_iadView.frame.size.height;
_iadView.frame = adFrame;

[_iadView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

[self.view addSubview:_iadView];

The above will size the iAd correctly and place it on the top of the screen.