How To Integrate iAd into iPhone App, wrong position of the banner

Member
Posts: 164
Joined: 2010.10
Post: #1
i have a cocos2d app, that musta have a banner. i have seen this: http://www.raywenderlich.com/1371/how-to...iphone-app

i have tried to use this example to make a view with only the banner, so i created a new UIViewController with xib, then add to a cocos2d view with:
Code:
banner=[[Banner alloc] initWithNibName:@"Banner" bundle:[NSBundle mainBundle]];
[[[CCDirector sharedDirector] openGLView] addSubview:[banner view]];

the unexpected result is: when i add this view, sometimes, randomly, the banner appears behind the status bar (partially covered by the status bar) and i can't explain why... the firs time the view appear is ALWAYS behind the status bar, so you'll see noted in code: //<<<<<<<<---------------------------------------note: i have putted this because the first time the window appear, it is alway behind the status bar

i have no contentView even if i have in this code, is not connected to any view
banner.h:
Code:
` #import #import "iAd/ADBannerView.h"

@interface Banner : UIViewController <UISplitViewControllerDelegate, ADBannerViewDelegate> {
   UIView *_contentView;
   id _adBannerView;
   BOOL _adBannerViewIsVisible;
    int firstTime;
}

@property (nonatomic, retain) IBOutlet UIView *contentView;
@property (nonatomic, retain) id adBannerView;
@property (nonatomic) BOOL adBannerViewIsVisible;

@end
banner.m
Code:
#import "Banner.h"

@implementation Banner

@synthesize contentView = _contentView;
@synthesize adBannerView = _adBannerView;
@synthesize adBannerViewIsVisible = _adBannerViewIsVisible;


- (int)getBannerHeight:(UIDeviceOrientation)orientation {
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        return 32;
    } else {
        return 50;
    }
}

- (int)getBannerHeight {
    return [self getBannerHeight:[UIDevice currentDevice].orientation];
}

- (void)createAdBannerView {
    Class classAdBannerView = NSClassFromString(@"ADBannerView");
    if (classAdBannerView != nil) {
        self.adBannerView = [[[classAdBannerView alloc]
                       initWithFrame:CGRectZero] autorelease];
        [_adBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:
                                            ADBannerContentSizeIdentifier320x50,
                                            ADBannerContentSizeIdentifier480x32, nil]];
        if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
            [_adBannerView setCurrentContentSizeIdentifier:
          ADBannerContentSizeIdentifier480x32];
        } else {
            [_adBannerView setCurrentContentSizeIdentifier:
          ADBannerContentSizeIdentifier320x50];          
        }
        [_adBannerView setFrame:CGRectOffset([_adBannerView frame], 0,
                                  -[self getBannerHeight])];
        [_adBannerView setDelegate:self];

        [self.view addSubview:_adBannerView];      
    }
}

- (void)fixupAdView:(UIInterfaceOrientation)toInterfaceOrientation {
    if (_adBannerView != nil) {      
        if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
            [_adBannerView setCurrentContentSizeIdentifier:
          ADBannerContentSizeIdentifier480x32];
        } else {
            [_adBannerView setCurrentContentSizeIdentifier:
          ADBannerContentSizeIdentifier320x50];
        }        
        [UIView beginAnimations:@"fixupViews" context:nil];
        if (_adBannerViewIsVisible) {
            CGRect adBannerViewFrame = [_adBannerView frame];
            adBannerViewFrame.origin.x = 0;
            adBannerViewFrame.origin.y = 0;
            if(firstTime==0){//<<<<-------------------------------------note: i have putted this because the first time the window appear, it is alway behind the status bar
                adBannerViewFrame.origin.y = 20;
                firstTime++;
            }
            [_adBannerView setFrame:adBannerViewFrame];
            CGRect contentViewFrame = _contentView.frame;
            contentViewFrame.origin.y =
         [self getBannerHeight:toInterfaceOrientation];
            contentViewFrame.size.height = self.view.frame.size.height -
         [self getBannerHeight:toInterfaceOrientation];
            _contentView.frame = contentViewFrame;
            NSLog(@"visible frame %f %f %f %f ",adBannerViewFrame.origin.x,adBannerViewFrame.origin.y,adBannerViewFrame.size.wi​dth,adBannerViewFrame.size.height);
        } else {
            CGRect adBannerViewFrame = [_adBannerView frame];
            adBannerViewFrame.origin.x = 0;
            adBannerViewFrame.origin.y = 0
         -[self getBannerHeight:toInterfaceOrientation];
            [_adBannerView setFrame:adBannerViewFrame];
            CGRect contentViewFrame = _contentView.frame;
            contentViewFrame.origin.y = 0;
            contentViewFrame.size.height = self.view.frame.size.height;
            _contentView.frame = contentViewFrame;
            NSLog(@"invisib frame %f %f %f %f ",adBannerViewFrame.origin.x,adBannerViewFrame.origin.y,adBannerViewFrame.size.wi​dth,adBannerViewFrame.size.height);
        }
        [UIView commitAnimations];
    }  
}


/*
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    NSLog(@"did load banner");

    firstTime=0;//<<<<<<<<---------------------------------------note: i have putted this because the first time the window appear, it is alway behind the status bar
   [self createAdBannerView];
    [super viewDidLoad];
}

- (void) viewWillAppear:(BOOL)animated {
    [self fixupAdView:[UIDevice currentDevice].orientation];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrient​ation duration:(NSTimeInterval)duration {
    [self fixupAdView:toInterfaceOrientation];
}

#pragma mark ADBannerViewDelegate

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    if (!_adBannerViewIsVisible) {              
        _adBannerViewIsVisible = YES;
        [self fixupAdView:[UIDevice currentDevice].orientation];
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if (_adBannerViewIsVisible)
    {      
        _adBannerViewIsVisible = NO;
        [self fixupAdView:[UIDevice currentDevice].orientation];
    }
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr​ientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
   self.contentView = nil;
   self.adBannerView = nil;  

}
@end

`
any ideas? thanks
Quote this message in a reply
Member
Posts: 96
Joined: 2011.07
Post: #2
[_adBannerView setFrame:CGRectOffset([_adBannerView frame], 0,
-[self getBannerHeight])];

This seems to set the frame of the banner. Run the program with the debugger and look at the values, for example, bannerHeight, when the problem occurs and when it doesn't.

respect,
pk

iFrog is coming.
Quote this message in a reply
Member
Posts: 164
Joined: 2010.10
Post: #3
tryed but even if i put a fixed frame, it sometimes go to the wrong position Sad
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  new position with vector pander007 3 3,703 Oct 13, 2007 11:49 AM
Last Post: Nevada
  geting window position marko 2 2,788 Jul 24, 2005 02:32 AM
Last Post: marko