Question

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?

Was it helpful?

Solution 2

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];

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top