1

I'm trying to get the rendered width of an ADBannerView but it always seems to be the same as my UIScreen's mainScreen's width:

adBannerView = [[ADBannerView alloc] init];
[self.navController.view addSubview:adBannerView];
NSLog(@"Banner's width: %f.", adBannerView.frame.size.width);
NSLog(@"Screen's width: %f.", [UIScreen mainScreen].bounds.size.width);

The two logs above show the same value. I want to eventually center my banner horizontally using the code below, but the width I get back from the banner's frame needs to be the rendered width:

adBannerView.frame = CGRectOffset(adBannerView.frame, ([UIScreen mainScreen].bounds.size.width - adBannerView.frame.size.width)/2.0f, 0);

So how do I get the rendered width of the ADBannerView?

Alexandru
  • 12,264
  • 17
  • 113
  • 208

2 Answers2

1

You can use these sizes for bannerView:

extern NSString * const ADBannerContentSizeIdentifier320x50;
extern NSString * const ADBannerContentSizeIdentifier480x32;
extern NSString * const ADBannerContentSizeIdentifierPortrait;
extern NSString * const ADBannerContentSizeIdentifierLandscape;

But there is another scope:

To resize a banner view, use sizeThatFits: on the banner view, specifying the bounds of the view that contains the banner view. Resize the banner view with the returned size. The banner view will be sized to the correct width and height of the current device and orientation. The following code snippet shows a possible implementation:

ADBannerView *myBannerView = <#Get a banner view#>;
UIView *myContainingView = <#Get the containing view#>;
NSSize newBannerSize = [myBannerView sizeThatFits:myContainingView];
[myBannerView setBounds:newBannerSize];

If you just want to center your BannerView:

adBannerView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - adBannerView.frame.size.width)/2,adBannerView.frame.origin.y,adBannerView.frame.size.width,adBannerView.frame.size.height);

I hope it helps.

Documentation about ADBannerView is available here.

Alexandru
  • 12,264
  • 17
  • 113
  • 208
Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42
  • No, this won't work. Setting the frame is not going to center it properly. When the ad comes in, it takes on a specific size. That's exactly what I'm trying to do above with CGRectOffset but describing how it doesn't work. Also, using the hardcoded sizes is deprecated which is why I targeted this question for iOS 7, so I don't really want to go down the road of using that option. Also, setBounds takes a CGRect, not an NSSize. – Alexandru Mar 01 '14 at 21:17
  • Also, even using sizeThatFits returns back the same value as [UIScreen mainScreen].bounds.size.width. Thus, going back to the problem of trying to center it again because the ad that is displayed does not take up the entire screen's width! – Alexandru Mar 01 '14 at 21:28
  • Looks like the reason why it wasn't working for me all this time is because my application delegate is inheriting the CCAppDelegate since I'm using Cocos2D and I wasn't comparing my app's width against the director's width. – Alexandru Mar 01 '14 at 22:15
  • This was deprecated in iOS 6, but Apple provided no alternative! – sudo Jun 19 '14 at 17:57
0

Just wanted to share a solution that worked for me. It looks like all my problems were due to my usage of Cocos2D. I shouldn't of used [UIScreen mainScreen].bounds.size.width. Here is what worked for me to properly, and finally, center the ad from my application's delegate (which inherits CCAppDelegate, so its like I was trying to mix-and-match two different interface sizing methodologies):

adBannerView = [[ADBannerView alloc] init];
adBannerView.backgroundColor = [UIColor whiteColor];
CGSize sizeToFit = [adBannerView sizeThatFits:[[CCDirector sharedDirector] view].frame.size];
// It is assumed that sizeThatFits returns size with a width smaller than the director's width, so just see if it needs to be centered.
[adBannerView setFrame:CGRectMake(([[CCDirector sharedDirector] view].frame.size.width - sizeToFit.width)/2.0f, 0, sizeToFit.width, sizeToFit.height)];
adBannerView.delegate = self;
[self.navController.view addSubview:adBannerView];
Alexandru
  • 12,264
  • 17
  • 113
  • 208