Search code examples
iosobjective-ccocos2d-iphoneiadadbannerview

How do you get an ADBannerView's rendered width in iOS 7?


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?


Solution

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